Skip to content

Commit

Permalink
Rename Logger to Log
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Jan 14, 2025
1 parent 2bc9c56 commit 8f84ae9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
19 changes: 10 additions & 9 deletions src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { Logger as PinoLogger } from "probot";
import type { Logger } from "probot";

/**
* A wrapper to the logger provided by Probot. This acts as a singleton.
*
* * `initialise()` - Pass the probot logger here.
* * `create()` - Creates and returns child loggers identified by `name`.
*/
export class Logger {
static pino: PinoLogger | null = null;
export class Log {
private static pino: Logger<never, boolean> | null = null;

static initialise(log: PinoLogger): void {
static initialise(log: Logger<never, boolean>): void {
if (this.pino !== null) {
throw Error("Logger has already been initialised!");
}
Expand All @@ -18,11 +15,15 @@ export class Logger {
log.info("Logger successfully initialised!");
}

static create(name: string): PinoLogger {
static create(name: string): Logger<never, boolean> {
if (this.pino === null) {
throw Error("Logger is not yet initialised. Run `Logger.initialise()` first!");
throw Error("Logger is not yet initialised. Run `Log.initialise()` first!");
}

return this.pino.child({ name });
}

static reset(): void {
this.pino = null;
}
}
22 changes: 10 additions & 12 deletions tests/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { pino } from "pino";
import { Probot } from "probot";
import { Stream } from "stream";
import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { Logger } from "../src/logger";
import { Log } from "../src/logger";

describe("Logger", () => {
describe("Log", () => {
let probot: Probot;
let output: Array<string> = [];

const writableStream = new Stream.Writable({ objectMode: true });
writableStream._write = (object, encoding, done) => {
writableStream._write = (object, _, done) => {
output.push(JSON.parse(object).msg);
done();
};
Expand All @@ -22,30 +22,28 @@ describe("Logger", () => {
});

afterEach(() => {
Logger.pino = null;
Log.reset();
});

test("Initialise cannot be called more than once.", () => {
Logger.initialise(probot.log);
Log.initialise(probot.log);

expect(() => Logger.initialise(probot.log)).toThrowError("Logger has already been initialised!");
expect(() => Log.initialise(probot.log)).toThrowError("Logger has already been initialised!");
});

test("Initialise logs correct message.", () => {
Logger.initialise(probot.log);
Log.initialise(probot.log);

expect(output.pop()).toBe("Logger successfully initialised!");
});

test("Cannot call create if initialise is not called yet.", () => {
expect(() => Logger.create("test")).toThrowError(
"Logger is not yet initialised. Run `Logger.initialise()` first!",
);
expect(() => Log.create("test")).toThrowError("Logger is not yet initialised. Run `Log.initialise()` first!");
});

test("Create returns logger instance.", () => {
Logger.initialise(probot.log);
Log.initialise(probot.log);

expect(Logger.create("test")).toBeInstanceOf(Object);
expect(Log.create("test")).toBeInstanceOf(Object);
});
});

0 comments on commit 8f84ae9

Please sign in to comment.