Skip to content

Commit 71db9ff

Browse files
fix(core-logger-pino): split log files per process (#4414)
1 parent d955559 commit 71db9ff

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

__tests__/unit/core-logger-pino/driver.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { Container, Identifiers } from "@packages/core-kernel/src/ioc";
77
import { PinoLogger } from "@packages/core-logger-pino/src/driver";
88
import capcon from "capture-console";
99
import { readdirSync } from "fs-extra";
10+
import { Writable } from "stream";
1011
import { dirSync, setGracefulCleanup } from "tmp";
1112

1213
let logger: Logger;
@@ -16,6 +17,7 @@ let app: Application;
1617

1718
beforeEach(async () => {
1819
app = new Application(new Container());
20+
app.bind(Identifiers.ConfigFlags).toConstantValue("core");
1921
app.bind(Identifiers.ApplicationNamespace).toConstantValue("ark-unitnet");
2022
app.bind("path.log").toConstantValue(dirSync().name);
2123

@@ -127,10 +129,39 @@ describe("Logger", () => {
127129
expect(message).toMatch(/non_silent_message/);
128130
});
129131

130-
// TODO: Re-enable tests
131-
// this passes locally but fails on pipelines
132-
it.skip("should rotate the log 3 times", async () => {
132+
it("should log error if there is an error on file stream", async () => {
133+
const logger = app.resolve<Logger>(PinoLogger);
134+
135+
const writableMock = new Writable({
136+
write(chunk, enc, cb) {
137+
throw new Error("Stream error");
138+
},
139+
});
140+
// @ts-ignore
141+
logger.getFileStream = () => {
142+
return writableMock;
143+
};
144+
145+
await logger.make({
146+
levels: {
147+
console: "invalid",
148+
file: process.env.CORE_LOG_LEVEL_FILE || "debug",
149+
},
150+
fileRotator: {
151+
interval: "1d",
152+
},
153+
});
154+
155+
writableMock.destroy(new Error("Test error"));
156+
157+
await sleep(100);
158+
159+
expect(message).toMatch("File stream closed due to an error: Error: Test error");
160+
});
161+
162+
it("should rotate the log 3 times", async () => {
133163
const app = new Application(new Container());
164+
app.bind(Identifiers.ConfigFlags).toConstantValue("core");
134165
app.bind(Identifiers.ApplicationNamespace).toConstantValue("ark-unitnet");
135166
app.useLogPath(dirSync().name);
136167

@@ -147,7 +178,7 @@ describe("Logger", () => {
147178
for (let i = 0; i < 3; i++) {
148179
logger.info(`Test ${i + 1}`);
149180

150-
await sleep(1000);
181+
await sleep(900);
151182
}
152183

153184
const files = readdirSync(app.logPath());

__tests__/unit/core-logger-pino/service-provider.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import "jest-extended";
22

3-
import { Providers } from "@arkecosystem/core-kernel";
4-
import { Application, Container, Services } from "@packages/core-kernel/src";
3+
import { Application, Container, Providers, Services } from "@packages/core-kernel";
54
import { ServiceProvider } from "@packages/core-logger-pino/src";
65
import { defaults } from "@packages/core-logger-pino/src/defaults";
76
import { AnySchema } from "joi";
@@ -11,6 +10,8 @@ let app: Application;
1110

1211
beforeEach(() => {
1312
app = new Application(new Container.Container());
13+
14+
app.bind(Container.Identifiers.ConfigFlags).toConstantValue("core");
1415
});
1516

1617
describe("ServiceProvider", () => {

packages/core-logger-pino/src/driver.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Container, Contracts, Utils } from "@arkecosystem/core-kernel";
22
import chalk, { Chalk } from "chalk";
3+
import * as console from "console";
34
import pino, { PrettyOptions } from "pino";
45
import PinoPretty from "pino-pretty";
56
import pump from "pump";
@@ -24,6 +25,9 @@ export class PinoLogger implements Contracts.Kernel.Logger {
2425
@Container.inject(Container.Identifiers.Application)
2526
private readonly app!: Contracts.Kernel.Application;
2627

28+
@Container.inject(Container.Identifiers.ConfigFlags)
29+
private readonly configFlags!: { processType: string };
30+
2731
/**
2832
* @private
2933
* @type {Record<string, Chalk>}
@@ -103,6 +107,10 @@ export class PinoLogger implements Contracts.Kernel.Logger {
103107
// @ts-ignore - Object literal may only specify known properties, and 'colorize' does not exist in type 'PrettyOptions'.
104108
this.createPrettyTransport(options.levels.console, { colorize: true }),
105109
process.stdout,
110+
/* istanbul ignore next */
111+
(err) => {
112+
console.error("Stdout stream closed due to an error:", err);
113+
},
106114
);
107115
}
108116

@@ -113,6 +121,9 @@ export class PinoLogger implements Contracts.Kernel.Logger {
113121
// @ts-ignore - Object literal may only specify known properties, and 'colorize' does not exist in type 'PrettyOptions'.
114122
this.createPrettyTransport(options.levels.file, { colorize: false }),
115123
this.fileStream,
124+
(err) => {
125+
console.error("File stream closed due to an error:", err);
126+
},
116127
);
117128
}
118129

@@ -262,7 +273,7 @@ export class PinoLogger implements Contracts.Kernel.Logger {
262273
return createStream(
263274
(time: number | Date, index?: number): string => {
264275
if (!time) {
265-
return `${this.app.namespace()}-current.log`;
276+
return `${this.app.namespace()}-${this.configFlags.processType}-current.log`;
266277
}
267278

268279
if (typeof time === "number") {
@@ -276,7 +287,7 @@ export class PinoLogger implements Contracts.Kernel.Logger {
276287
filename += `.${index}`;
277288
}
278289

279-
return `${this.app.namespace()}-${filename}.log.gz`;
290+
return `${this.app.namespace()}-${this.configFlags.processType}-${filename}.log.gz`;
280291
},
281292
{
282293
path: this.app.logPath(),

0 commit comments

Comments
 (0)