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

add error handler to file rotation #2633

Merged
merged 3 commits into from
Mar 22, 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
57 changes: 36 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"semver": "^7.5.2",
"triple-beam": "^1.3.0",
"winston": "^3.7.2",
"winston-daily-rotate-file": "^4.7.1",
"winston-daily-rotate-file": "^5.0.0",
"winston-transport": "^4.5.0"
},
"keywords": [
Expand Down
39 changes: 21 additions & 18 deletions packages/common/src/lib/common/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,7 @@ export function logger(
if (userOptions.transport) {
let fName = 0;
const isWindows = os.platform().startsWith('win');
Object.keys(userOptions.transport).forEach(f => {
const transport = userOptions.transport[f];

for (const transport of Object.values(userOptions.transport)) {
transport._defaultConfigLoglevel = transport.level; // remember Loglevel if set
transport.level = transport.level || level;

Expand Down Expand Up @@ -251,6 +249,11 @@ export function logger(

try {
const _log = new DailyRotateFile(transport);

_log.on('error', err => {
console.error(`Error on log file rotation: ${err.message}`);
});

options.transports.push(_log);
} catch (e) {
if (e.code === 'EACCES') {
Expand All @@ -262,7 +265,7 @@ export function logger(
} else if (transport.type === 'syslog' && transport.enabled !== false) {
if (!IoSysLog) {
console.error('Syslog configured, but not installed! Ignore');
return;
continue;
}
// host: The host running syslogd, defaults to localhost.
// port: The port on the host that syslog is running on, defaults to syslogd's default port.
Expand All @@ -287,8 +290,8 @@ export function logger(
}
try {
options.transports.push(new IoSysLog(transport));
} catch (err) {
console.log(`Cannot activate Syslog: ${err.message}`);
} catch (e) {
console.error(`Cannot activate Syslog: ${e.message}`);
}
} else if (transport.type === 'http' && transport.enabled !== false) {
// host: (Default: localhost) Remote host of the HTTP logging endpoint
Expand All @@ -302,8 +305,8 @@ export function logger(

try {
options.transports.push(new winston.transports.Http(transport));
} catch (err) {
console.log(`Cannot activate HTTP: ${err.message}`);
} catch (e) {
console.error(`Cannot activate HTTP: ${e.message}`);
}
} else if (transport.type === 'stream' && transport.enabled !== false) {
// stream: any Node.js stream. If an objectMode stream is provided then the entire info object will be written. Otherwise info[MESSAGE] will be written.
Expand All @@ -318,18 +321,18 @@ export function logger(
if (typeof transport.stream === 'string') {
transport.stream = fs.createWriteStream(transport.stream);
transport.stream.on('error', (err: Error) => {
console.log(`Error in Stream: ${err.message}`);
console.error(`Error in Stream: ${err.message}`);
});
}

options.transports.push(new winston.transports.Stream(transport));
} catch (err) {
console.log(`Cannot activate Stream: ${err.message}`);
} catch (e) {
console.error(`Cannot activate Stream: ${e.message}`);
}
} else if (transport.type === 'seq' && transport.enabled !== false) {
if (!IoSeq) {
console.error('Seq configured, but not installed! Ignore');
return;
continue;
}
// serverUrl?: string;
// apiKey?: string;
Expand All @@ -345,14 +348,14 @@ export function logger(
};
const seqLogger = new IoSeq(transport);
options.transports.push(seqLogger);
} catch (err) {
console.log(`Cannot activate SEQ: ${err.message}`);
} catch (e) {
console.error(`Cannot activate SEQ: ${e.message}`);
}
} else {
console.log('Cannot activate SEQ: No serverUrl specified');
console.error('Cannot activate SEQ: No serverUrl specified');
}
}
});
}
}
} else {
for (let i = 0; i < files.length; i++) {
Expand Down Expand Up @@ -454,8 +457,8 @@ export function logger(
try {
/** @ts-expect-error we use undocumented stuff here TODO */
files = fs.readdirSync(transport.dirname);
} catch (err) {
console.log(`host.${hostname} Cannot read log directory: ${err}`);
} catch (e) {
console.error(`host.${hostname} Cannot read log directory: ${e.message}`);
return;
}
const forXdays = new Date();
Expand Down
Loading