Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix logger crashing on restart #22565

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class Controller {
private extensionArgs: ExtensionArgs;

constructor(restartCallback: () => void, exitCallback: (code: number, restart: boolean) => void) {
logger.init();
zhSetLogger(logger);
zhcSetLogger(logger);
this.eventBus = new EventBus();
Expand Down
10 changes: 5 additions & 5 deletions lib/util/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type LogLevel = typeof LOG_LEVELS[number];

class Logger {
private level: LogLevel;
private readonly output: string[];
private readonly directory: string;
private readonly logger: winston.Logger;
private readonly fileTransport: winston.transports.FileTransportInstance;
private output: string[];
private directory: string;
private logger: winston.Logger;
private fileTransport: winston.transports.FileTransportInstance;
private debugNamespaceIgnoreRegex?: RegExp;

constructor() {
public init(): void {
// What transports to enable
this.output = settings.get().advanced.log_output;
// Directory to log to
Expand Down
25 changes: 10 additions & 15 deletions test/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {rimrafSync} = require('rimraf');
const Transport = require('winston-transport');

describe('Logger', () => {
let logger;

beforeEach(async () => {
data.writeDefaultConfiguration();
jest.resetModules();
Expand All @@ -17,21 +19,20 @@ describe('Logger', () => {
settings.reRead();
stdOutWriteOriginal = console._stdout.write;
console._stdout.write = () => {};
logger = require('../lib/util/logger').default;
logger.init();
});

afterEach(async () => {
console._stdout.write = stdOutWriteOriginal;
});

it('Create log directory', () => {
const logger = require('../lib/util/logger').default;
const dirs = fs.readdirSync(dir.name);
expect(dirs.length).toBe(1);
});

it('Should cleanup', () => {
const logger = require('../lib/util/logger').default;

for (const d of fs.readdirSync(dir.name)) {
rimrafSync(path.join(dir.name, d));
}
Expand All @@ -46,7 +47,6 @@ describe('Logger', () => {
})

it('Should not cleanup when there is no timestamp set', () => {
const logger = require('../lib/util/logger').default;
for (let i = 30; i < 40; i++) {
fs.mkdirSync(path.join(dir.name, `log_${i}`));
}
Expand All @@ -58,7 +58,6 @@ describe('Logger', () => {
})

it('Set and get log level', () => {
const logger = require('../lib/util/logger').default;
logger.setLevel('debug');
expect(logger.getLevel()).toBe('debug');
logger.setLevel('info');
Expand All @@ -74,7 +73,7 @@ describe('Logger', () => {
it('Set warning when log level is warn', () => {
settings.set(['advanced', 'log_level'], 'warn');
settings.reRead();
const logger = require('../lib/util/logger').default;
logger.init();
expect(logger.level).toBe('warning');// getLevel() reports old Z2M level to match display/value
settings.set(['advanced', 'log_level'], 'info');
settings.reRead();
Expand All @@ -86,7 +85,6 @@ describe('Logger', () => {
}
}

const logger = require('../lib/util/logger').default;
expect(logger.winston.transports.length).toBe(2);
const transport = new DummyTransport();
logger.addTransport(transport);
Expand All @@ -96,7 +94,6 @@ describe('Logger', () => {
});

it('Logger should be console and file by default', () => {
const logger = require('../lib/util/logger').default;
const pipes = logger.winston._readableState.pipes;
expect(pipes.length).toBe(2);
expect(pipes[0].constructor.name).toBe('Console');
Expand All @@ -107,7 +104,7 @@ describe('Logger', () => {

it('Logger can be file only', () => {
settings.set(['advanced', 'log_output'], ['file']);
const logger = require('../lib/util/logger').default;
logger.init();
const pipes = logger.winston._readableState.pipes;
expect(pipes.length).toBe(2);
expect(pipes[0].constructor.name).toBe('Console');
Expand All @@ -118,23 +115,23 @@ describe('Logger', () => {

it('Logger can be console only', () => {
settings.set(['advanced', 'log_output'], ['console']);
const logger = require('../lib/util/logger').default;
logger.init();
const pipes = logger.winston._readableState.pipes;
expect(pipes.constructor.name).toBe('Console');
expect(pipes.silent).toBe(false);
});

it('Logger can be nothing', () => {
settings.set(['advanced', 'log_output'], []);
const logger = require('../lib/util/logger').default;
logger.init();
const pipes = logger.winston._readableState.pipes;
expect(pipes.constructor.name).toBe('Console');
expect(pipes.silent).toBe(true);
});

it('Should allow to disable log rotation', () => {
settings.set(['advanced', 'log_rotation'], false);
const logger = require('../lib/util/logger').default;
logger.init();
const pipes = logger.winston._readableState.pipes;
expect(pipes[1].constructor.name).toBe('File');
expect(pipes[1].maxFiles).toBeNull();
Expand All @@ -144,15 +141,14 @@ describe('Logger', () => {

it('Should allow to symlink logs to current directory', () => {
settings.set(['advanced', 'log_symlink_current'], true);
let logger = require('../lib/util/logger').default;
logger.init();
expect(fs.readdirSync(dir.name).includes('current')).toBeTruthy()

jest.resetModules();
logger = require('../lib/util/logger').default;
});

it('Log', () => {
const logger = require('../lib/util/logger').default;
logger.setLevel('debug');

const debug = jest.spyOn(logger.winston, 'debug');
Expand Down Expand Up @@ -221,7 +217,6 @@ describe('Logger', () => {
]
],
])('Sets namespace ignore for debug level %s', (ignore, expected, tests) => {
const logger = require('../lib/util/logger').default;
logger.setLevel('debug');
const debugSpy = jest.spyOn(logger.winston, 'debug');
logger.setDebugNamespaceIgnore(ignore);
Expand Down