@midwayjs/logger is a log module for midway project.
$ npm install @midwayjs/logger --save
import { loggers } from '@midwayjs/logger';
const logger = loggers.createLogger('logger', {
// some logger options
})
Create logger with console and file transports instance.
import { loggers, ConsoleTransport, FileTransport } from '@midwayjs/logger';
const logger = loggers.createLogger('logger', {
transports: {
console: new ConsoleTransport(),
file: new FileTransport({
dir: '...',
fileLogName: 'app.log',
}),
}
})
Create console logger.
const logger = loggers.createLogger('consoleLogger', {
transports: {
console: new ConsoleTransport(),
}
})
Create logger with options mode.
const logger = loggers.createLogger('consoleLogger', {
transports: {
console: {
autoColors: true,
},
file: {
dir: '...',
fileLogName: 'app.log',
}
}
})
logger.debug('debug info');
logger.info('启动耗时 %d ms', Date.now() - start);
logger.warn('warning!');
logger.error(new Error('my error'));
logger.write('abcde');
log level is divided into the following categories, and the log level decreases sequentially (the larger the number, the lower the level):
const levels = {
none: 0,
error: 1,
trace: 2,
warn: 3,
info: 4,
verbose: 5,
debug: 6,
silly: 7,
all: 8,
}
Set level for all transports
const logger = loggers.createLogger('logger', {
// ...
level: 'warn',
});
// not output
logger.debug('debug info');
// not output
logger.info('debug info');
Add logger format and context format.
const logger = loggers.createLogger('logger', {
// ...
format: info => {
return `${info.timestamp} ${info.message}`;
},
contextFormat: info => {
return `${info.timestamp} [${info.ctx.traceId}] ${info.message}`;
}
});
info is a default metadata, include some properties.
The actual behavior of the log output we call the transport.The log library has four built-in default Transports.
ConsoleTransport
Output message to stdout and stderr with color.FileTransport
Output message to file and rotate by self.ErrorTransport
InheritFileTransport
and only output error message.JSONTransport
InheritFileTransport
and output json format.
The above Transports are all registered by default and can be configured by the name when registering.
const logger = loggers.createLogger('consoleLogger', {
transports: {
console: {/*...options*/},
file: {/*...options*/},
error: {/*...options*/},
json: {/*...options*/},
}
});
Inherit Transport abstract class and implement log
and close
method.
import { Transport, ITransport } from '@midwayjs/logger';
export interface CustomTransportOptions {
// ...
}
export class CustomTransport extends Transport<CustomTransportOptions> implements ITransport {
log(level: LoggerLevel | false, meta: LogMeta, ...args) {
// save file or post to remote server
}
close() {}
}
Register class to TransportManager
before used.
import { TransportManager } from '@midwayjs/logger';
TransportManager.set('custom', CustomTransport);
And you can configure it in your code.
const logger = loggers.createLogger('consoleLogger', {
transports: {
custom: {/*...options*/}
}
});
find more options in interface.
MIT