Skip to content

Commit

Permalink
feat: logger rc.1
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszherba committed Oct 4, 2024
1 parent 287f494 commit ead0b10
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-jokes-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vue-storefront/logger": minor
---

rc.1
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
module.exports = {
extends: "@vue-storefront/eslint-config-integrations",
rules: {
"class-methods-use-this": "off",
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ describe("GCPStructuredLog", () => {
},
},
{
id: "9",
id: "9: with metadata",
logData: "message",
options: { includeStackTrace: false },
severity: "info",
Expand All @@ -132,6 +132,23 @@ describe("GCPStructuredLog", () => {
labels: "other-label",
},
},
{
id: "10:with ansi codes",
logData:
"\x1b[90m(/var/www/apps/storefront-unified-nextjs/\x1b[39m.next/server/chunks/689.js:67:9\x1b[90m)\x1b[39m",
options: { includeStackTrace: false },
severity: "info",
metadata: {
labels: "other-label",
},
expected: {
message:
"(/var/www/apps/storefront-unified-nextjs/.next/server/chunks/689.js:67:9)",
severity: "INFO",
timestamp: expect.any(String),
labels: "other-label",
},
},
])(
"should create a GCP structured log: #ID $id",
({ logData, expected, options, severity, metadata }) => {
Expand Down
7 changes: 5 additions & 2 deletions packages/logger/src/ConsolaStructuredLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import consola, {
} from "consola";

import dotenv from "dotenv";

import { LogLevel } from "./interfaces/LogLevel";
import type { LogData, Logger, Metadata } from "./interfaces/Logger";
import type { LoggerOptions } from "./interfaces/LoggerOptions";
import type { StructuredLog } from "./interfaces/StructuredLog";
import type { StructuredLogger } from "./interfaces/StructuredLogger";

dotenv.config();
// We do not want to load the .env in the browser and in the edge runtime
if (typeof window === "undefined" && process.env.NEXT_RUNTIME !== "edge") {
dotenv.config();
}

interface ConsolaLoggerOptions
extends LoggerOptions,
Partial<Omit<ConsolaOptions, "level">> {}
Expand Down
9 changes: 8 additions & 1 deletion packages/logger/src/LoggerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ export enum LoggerType {
* @param type The type of logger to create
*/
export class LoggerFactory {
static create(type: LoggerType, options: LoggerOptions = {}) {
static create(
type: LoggerType,
options: LoggerOptions = {
level: "info",
includeStackTrace: true,
environment: "production",
}
) {
switch (type) {
case LoggerType.ConsolaGcp:
return new ConsolaStructuredLogger(new GCPStructuredLog(), options);
Expand Down
7 changes: 7 additions & 0 deletions packages/logger/src/interfaces/LoggerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { LogLevel } from "./LogLevel";

export type Environment = "production" | "dev" | "stage" | "test";

/**
* Options for the logger.
*/
Expand All @@ -13,4 +15,9 @@ export interface LoggerOptions {
* Whether to include the stack trace in the log message.
*/
includeStackTrace?: boolean;

/**
* The environment in which the logger is running.
*/
environment?: Environment;
}
26 changes: 20 additions & 6 deletions packages/logger/src/structuredLog/GCPStructuredLog.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
removeAnsiCodesFromStr as defaultRemoveAnsiCodes,
type RemoveAnsiCode,
} from "../utils/removeAnsiCodes";
import type { LogData, Metadata } from "../interfaces/Logger";
import { GCPStructuredDTO } from "../interfaces/gcp/GCPStructuredLogger";
import { StructuredLog } from "../interfaces/StructuredLog";
Expand Down Expand Up @@ -35,6 +39,15 @@ export class GCPStructuredLog implements StructuredLog {
debug: "DEBUG",
};

/**
* The function to remove ANSI codes from the log message.
*/
private removeAnsiCodes: RemoveAnsiCode;

constructor(removeAnsiCodes: RemoveAnsiCode = defaultRemoveAnsiCodes) {
this.removeAnsiCodes = removeAnsiCodes;
}

/**
* Creates a structured log object for GCP.
*/
Expand Down Expand Up @@ -84,15 +97,16 @@ export class GCPStructuredLog implements StructuredLog {
* @returns The formatted log message.
*/
private formatMessage(logData: LogData): string {
let message = "";
if (logData instanceof Error) {
return logData.message;
}

if (typeof logData === "string") {
return logData;
message = logData.message;
} else if (typeof logData === "string") {
message = logData;
} else {
message = JSON.stringify(logData);
}

return JSON.stringify(logData);
return this.removeAnsiCodes(message);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/logger/src/utils/removeAnsiCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type RemoveAnsiCode = (str: string) => string;

export const removeAnsiCodesFromStr: RemoveAnsiCode = (str) => {
// eslint-disable-next-line no-control-regex
const ansiRegex = /\x1B\[[0-?9;]*[mK]/g;

return str.replace(ansiRegex, "");
};
1 change: 1 addition & 0 deletions packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"declaration": true,
"rootDir": "./src",
"experimentalDecorators": true

},
"exclude": ["node_modules"],
"include": ["src/**/*.ts"]
Expand Down

0 comments on commit ead0b10

Please sign in to comment.