@evojs/logger
v5.0.0
Published
Nodejs logger
Downloads
870
Readme
@evojs/logger
Smart logger for nodejs
Usage example
Set global configuration
import { Callsite, Logger, Level, Record } from '@evojs/logger';
const accessLogFileStream = createWriteStream(accessLogFile, { flags: 'a' });
const errorLogFileStream = createWriteStream(errorLogFile, { flags: 'a' });
Logger.configure({
name: 'app',
formats: [
`{{ date | date }} {{ level | uppercase }}{{ name | name }} {{ args | message }}<-|->{{ callsite | file }}`,
'json',
],
pipes: {
uppercase(text: string): string {
return text.toUpperCase();
},
date(date: number): string {
return new Date(date).toISOString();
},
name(name: string): string {
return name ? ` <${name}>` : '';
},
message(args: any[]): string {
return args
.map((x) =>
typeof x === 'string' ? x : x instanceof Error ? x.stack : inspect(x, false, null, false),
)
.join('\n');
},
file({ source, line, column }: Callsite = {}): string {
return [source, line, column].filter(Boolean).join(':');
},
},
handler(record: Record): void {
const [customOutput, jsonOutput] = record.messages();
// 2 formats => 2 outputs
if (!errorLevels.includes(record.level)) {
process.stdout.write(customOutput + '\n');
accessLogFileStream.write(jsonOutput + '\n');
} else {
process.stderr.write(customOutput + '\n');
errorLogFileStream.write(jsonOutput + '\n');
}
},
});
Changing separator mask
import { Logger, Log } from '@evojs/logger';
Log.separator = '<=!=>';
Logger.configure({
formats: [`{{ date }}<=!=>{{ args | message }}`],
pipes: {
message(args: any[]): string {
return args.join(' ');
},
},
});
Creating new logger instance
import { Logger } from '@evojs/logger';
const logger = new Logger({ name: 'request' });
export const requestLogger = responseTime((req: any, res: any, time: number) => {
logger.info(
chalk.green(req.method),
chalk.yellow(res.statusCode),
req.url,
chalk.yellow(time.toFixed(0) + 'ms'),
chalk.green(
`${
req.headers['x-forwarded-for'] || req.headers['x-real-ip'] || req.connection.remoteAddress
}`,
),
chalk.magenta(req.headers['user-agent']),
);
});
Main features
- Very flexible and easy to understand configuration
- Metadata providing
- Caller information
- Console overriding
- Typescript typings
License
Licensed under MIT license