Skip to content

Commit

Permalink
feat: logger 1.0.0-rc.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszherba committed Oct 3, 2024
1 parent d36a288 commit bc2c84c
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 111 deletions.
61 changes: 45 additions & 16 deletions packages/logger/__tests__/unit/consolaStructuredLogger.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ConsolaStructuredLogger } from "../../src/ConsolaStructuredLogger";
import { LogLevel } from "../../src/interfaces/LogLevel";
import { StructuredLog } from "../../src/interfaces/StructuredLog";

// We do not want to serialize the log payload at this point
process.env.NODE_ENV = "dev";

describe("ConsolaStructuredLogger", () => {
let logger: ConsolaStructuredLogger;
let structuredLog: StructuredLog;
Expand All @@ -18,14 +20,18 @@ describe("ConsolaStructuredLogger", () => {
});

it("should log structured data at the specified level", () => {
const logData = { message: "test message" };
const level = LogLevel.Info;
const logData = "test message";
const level = "info";

logger.logStructured = jest.fn();

logger.log(level, logData);

expect(logger.logStructured).toHaveBeenCalledWith(level, logData);
expect(logger.logStructured).toHaveBeenCalledWith(
level,
logData,
undefined
);
});

it("should log at the emergency level", () => {
Expand All @@ -36,8 +42,9 @@ describe("ConsolaStructuredLogger", () => {
logger.emergency(logData);

expect(logger.logStructured).toHaveBeenCalledWith(
LogLevel.Emergency,
logData
"emergency",
logData,
undefined
);
});

Expand All @@ -48,7 +55,11 @@ describe("ConsolaStructuredLogger", () => {

logger.alert(logData);

expect(logger.logStructured).toHaveBeenCalledWith(LogLevel.Alert, logData);
expect(logger.logStructured).toHaveBeenCalledWith(
"alert",
logData,
undefined
);
});

it("should log at the critical level", () => {
Expand All @@ -59,8 +70,9 @@ describe("ConsolaStructuredLogger", () => {
logger.critical(logData);

expect(logger.logStructured).toHaveBeenCalledWith(
LogLevel.Critical,
logData
"critical",
logData,
undefined
);
});

Expand All @@ -71,7 +83,11 @@ describe("ConsolaStructuredLogger", () => {

logger.error(logData);

expect(logger.logStructured).toHaveBeenCalledWith(LogLevel.Error, logData);
expect(logger.logStructured).toHaveBeenCalledWith(
"error",
logData,
undefined
);
});

it("should log at the warning level", () => {
Expand All @@ -82,8 +98,9 @@ describe("ConsolaStructuredLogger", () => {
logger.warning(logData);

expect(logger.logStructured).toHaveBeenCalledWith(
LogLevel.Warning,
logData
"warning",
logData,
undefined
);
});

Expand All @@ -94,17 +111,25 @@ describe("ConsolaStructuredLogger", () => {

logger.notice(logData);

expect(logger.logStructured).toHaveBeenCalledWith(LogLevel.Notice, logData);
expect(logger.logStructured).toHaveBeenCalledWith(
"notice",
logData,
undefined
);
});

it("should log at the info level", () => {
const logData = "info log";

logger.logStructured = jest.fn();

logger.info(logData);
logger.info(logData, undefined);

expect(logger.logStructured).toHaveBeenCalledWith(LogLevel.Info, logData);
expect(logger.logStructured).toHaveBeenCalledWith(
"info",
logData,
undefined
);
});

it("should log at the debug level", () => {
Expand All @@ -114,6 +139,10 @@ describe("ConsolaStructuredLogger", () => {

logger.debug(logData);

expect(logger.logStructured).toHaveBeenCalledWith(LogLevel.Debug, logData);
expect(logger.logStructured).toHaveBeenCalledWith(
"debug",
logData,
undefined
);
});
});
102 changes: 85 additions & 17 deletions packages/logger/__tests__/unit/structuredLog/gcpStructuredLog.spec.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,147 @@
import { LogLevel } from "../../../src/interfaces/LogLevel";
import { GCPStructuredLog } from "../../../src/structuredLog/GCPStructuredLog";

// We do not want to serialize the log payload at this point
process.env.NODE_ENV = "dev";

describe("GCPStructuredLog", () => {
it.each([
{
id: "1",
logData: "test message",
options: {},
severity: LogLevel.Alert,
severity: "alert",
metadata: {
alokai: "test",
},
expected: {
message: "test message",
severity: "ALERT",
timestamp: expect.any(String),
trace: undefined,
alokai: "test",
},
},
{
logData: { message: "test message" },
id: "2",
logData: "test message",
options: { includeStackTrace: true },
severity: undefined,
metadata: {
alokai: "test",
},
expected: {
message: '{"message":"test message"}',
message: "test message",
severity: "DEFAULT",
timestamp: expect.any(String),
trace: undefined,
alokai: "test",
},
},
{
id: "3",
logData: new Error("test error"),
options: { includeStackTrace: true },
severity: undefined,
expected: {
message: "test error",
severity: "DEFAULT",
timestamp: expect.any(String),
trace: expect.any(String),
stackTrace: expect.any(String),
},
},
{
id: "4",
logData: "another test message",
options: { includeStackTrace: false },
severity: LogLevel.Info,
severity: "info",
metadata: {
alokai: "test",
},
expected: {
message: "another test message",
severity: "INFO",
timestamp: expect.any(String),
trace: undefined,
alokai: "test",
},
},
{
logData: { message: "test message with options" },
id: "5",
logData: "test message with options",
options: { includeStackTrace: false },
severity: LogLevel.Debug,
severity: "debug",
expected: {
message: '{"message":"test message with options"}',
message: "test message with options",
severity: "DEBUG",
timestamp: expect.any(String),
trace: undefined,
},
},
{
id: "6",
logData: new Error("another test error"),
options: { includeStackTrace: true },
severity: "error",
metadata: {
alokai: "test",
},
expected: {
message: "another test error",
severity: "ERROR",
timestamp: expect.any(String),
stackTrace: expect.any(String),
alokai: "test",
},
},
{
id: "7",
logData: new Error("another test error"),
options: { includeStackTrace: true },
severity: LogLevel.Error,
severity: "error",
metadata: {
alokai: "test",
},
expected: {
message: "another test error",
severity: "ERROR",
timestamp: expect.any(String),
trace: expect.any(String),
stackTrace: expect.any(String),
alokai: "test",
},
},
{
id: "8",
logData: "test message with options",
options: { includeStackTrace: false },
severity: "debug",
expected: {
message: "test message with options",
severity: "DEBUG",
timestamp: expect.any(String),
},
},
{
id: "9",
logData: "message",
options: { includeStackTrace: false },
severity: "info",
metadata: {
labels: "other-label",
},
expected: {
message: "message",
severity: "INFO",
timestamp: expect.any(String),
labels: "other-label",
},
},
])(
"should create a GCP structured log",
({ logData, expected, options, severity }) => {
"should create a GCP structured log: #ID $id",
({ logData, expected, options, severity, metadata }) => {
const log = new GCPStructuredLog();
const gcpStructuredLog = log.createLog(logData, options, severity as any);
const gcpStructuredLog = log.createLog(
logData,
options,
severity as LogLevel,
metadata
);
expect(gcpStructuredLog).toEqual(expected);
}
);
Expand Down
5 changes: 3 additions & 2 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vue-storefront/logger",
"version": "1.0.0",
"version": "0.0.0",
"main": "lib/index.cjs.js",
"module": "lib/index.es.js",
"types": "lib/index.d.ts",
Expand All @@ -23,6 +23,7 @@
"node": ">=18"
},
"dependencies": {
"consola": "^3"
"consola": "^3",
"dotenv": "^16"
}
}
Loading

0 comments on commit bc2c84c

Please sign in to comment.