@nexe/logger
Version:
Nexe Logger - A powerful logging solution based on Pino
156 lines • 5.65 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 ? { ...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 (arg instanceof Error) {
// 如果是Error对象,添加到err字段让pino处理
logObj.err = 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 if (arg instanceof Error) {
// 如果是Error对象,添加到err字段让pino处理
logObj.err = arg;
}
}
}
else if (obj instanceof Error) {
// logger.info(error, 'message') - 错误在前,消息在后
logObj.err = obj;
logMessage = message;
// 处理额外参数中的普通对象
for (const arg of args) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
Object.assign(logObj, arg);
}
else if (arg instanceof Error) {
// 多个错误对象的情况,可以用数组或者其他字段名
if (!logObj.additionalErrors) {
logObj.additionalErrors = [];
}
logObj.additionalErrors.push(arg);
}
}
}
else {
// logger.info() - 没有参数,或者 obj 是其他类型
logMessage = message;
// 处理额外参数中的普通对象
for (const arg of args) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
Object.assign(logObj, arg);
}
else if (arg instanceof Error) {
// 如果是Error对象,添加到err字段让pino处理
logObj.err = 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) {
// 统一处理所有情况,避免错误对象重复处理
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);
// 如果有对象数据,传递给 pino 作为第一个参数
if (logObj && Object.keys(logObj).length > 0) {
this._pino.info(logObj, msg);
}
else {
// 只有消息,直接传递
this._pino.info(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