@hestjs/logger
Version:
HestJS Logger - A powerful logging solution based on Pino
131 lines • 4.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HestLogger = void 0;
/**
* HestJS Logger 实现
*/
class HestLogger {
constructor(pinoLogger) {
this._context = {};
this._pino = pinoLogger;
}
/**
* 获取原始 Pino Logger 实例
*/
get pino() {
return this._pino;
}
/**
* 设置日志上下文
*/
setContext(context) {
this._context = { ...this._context, ...context };
return this;
}
/**
* 获取子 Logger
*/
child(bindings) {
const childPino = this._pino.child({ ...this._context, ...bindings });
const childLogger = new HestLogger(childPino);
childLogger._context = { ...this._context };
return childLogger;
}
/**
* 构建日志对象
*/
buildLogObject(obj, message, ...args) {
const context = Object.keys(this._context).length > 0 ? this._context : undefined;
let logObj = { ...context };
let logMessage;
if (typeof obj === 'string') {
// logger.info('message') 或 logger.info('message', data, ...)
logMessage = obj;
// 检查后续参数中是否有普通对象数据(不处理Error对象)
if (message !== undefined || args.length > 0) {
const allArgs = [message, ...args].filter(arg => arg !== undefined);
for (const arg of allArgs) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
// 如果是普通对象,合并到日志对象中
Object.assign(logObj, arg);
}
}
}
}
else if (obj && typeof obj === 'object' && !(obj instanceof Error)) {
// logger.info({ key: 'value' }, 'message') - 数据在前,消息在后
Object.assign(logObj, obj);
logMessage = message;
// 处理额外参数中的普通对象
for (const arg of args) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
Object.assign(logObj, arg);
}
}
}
else {
// logger.info() - 没有参数,或者 obj 是其他类型(包括Error)
logMessage = message;
// 处理额外参数中的普通对象
for (const arg of args) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
Object.assign(logObj, arg);
}
}
}
return [logObj, logMessage];
}
fatal(obj, message, ...args) {
const [logObj, msg] = this.buildLogObject(obj, message, ...args);
this._pino.fatal(logObj, msg);
}
error(obj, message, ...args) {
if (typeof obj === 'string' && message instanceof Error) {
// logger.error('message', error) - 消息在前,错误在后
// 直接让pino处理:传递Error对象作为合并对象,消息作为第二个参数
this._pino.error(message, obj);
}
else if (obj instanceof Error) {
// logger.error(error, 'message') - 错误在前,消息在后
// 直接让pino处理:传递Error对象作为合并对象,消息作为第二个参数
this._pino.error(obj, message);
}
else {
// 其他情况使用原来的方法
const [logObj, msg] = this.buildLogObject(obj, message, ...args);
this._pino.error(logObj, msg);
}
}
warn(obj, message, ...args) {
const [logObj, msg] = this.buildLogObject(obj, message, ...args);
this._pino.warn(logObj, msg);
}
info(obj, message, ...args) {
const [logObj, msg] = this.buildLogObject(obj, message, ...args);
this._pino.info(logObj, msg);
}
debug(obj, message, ...args) {
const [logObj, msg] = this.buildLogObject(obj, message, ...args);
this._pino.debug(logObj, msg);
}
trace(obj, message, ...args) {
const [logObj, msg] = this.buildLogObject(obj, message, ...args);
this._pino.trace(logObj, msg);
}
/**
* 刷新日志
*/
flush() {
if (typeof this._pino.flush === 'function') {
this._pino.flush();
}
}
/**
* 检查是否启用了指定级别
*/
isLevelEnabled(level) {
return this._pino.isLevelEnabled(level);
}
}
exports.HestLogger = HestLogger;
//# sourceMappingURL=logger.js.map