Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@hestjs/logger

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hestjs/logger - npm Package Compare versions

Comparing version
0.1.4
to
0.1.5
+9
-6
dist/factory.js

@@ -43,3 +43,3 @@ "use strict";

colorize: true,
ignore: "pid,hostname",
ignore: "pid,hostname,error", // 忽略重复的error字段,只显示err
translateTime: "yy-mm-dd HH:MM:ss.l",

@@ -67,4 +67,4 @@ messageFormat: "{msg}",

level: finalConfig.level || "debug",
serializers: finalConfig.serializers,
formatters: finalConfig.formatters,
serializers: finalConfig.serializers || {},
formatters: finalConfig.formatters || {},
};

@@ -78,8 +78,11 @@ pinoLogger = (0, pino_1.default)(pinoConfig, stream);

level: finalConfig.level || "info",
serializers: finalConfig.serializers,
formatters: finalConfig.formatters,
serializers: finalConfig.serializers || {},
formatters: finalConfig.formatters || {},
});
}
// 如果有 name,创建一个 child logger 来包含 name
const loggerName = name || finalConfig.name || "HestJS";
const loggerWithName = pinoLogger.child({ name: loggerName });
// 返回 HestLogger 实例
return new logger_1.HestLogger(pinoLogger);
return new logger_1.HestLogger(loggerWithName);
}

@@ -86,0 +89,0 @@ /**

+6
-6

@@ -22,8 +22,8 @@ import type { Logger, LoggerConfig } from './types';

export declare const logger: {
fatal: (obj?: object | string, message?: string, ...args: any[]) => void;
error: (obj?: object | string, message?: string, ...args: any[]) => void;
warn: (obj?: object | string, message?: string, ...args: any[]) => void;
info: (obj?: object | string, message?: string, ...args: any[]) => void;
debug: (obj?: object | string, message?: string, ...args: any[]) => void;
trace: (obj?: object | string, message?: string, ...args: any[]) => void;
fatal: Logger["fatal"];
error: Logger["error"];
warn: Logger["warn"];
info: Logger["info"];
debug: Logger["debug"];
trace: Logger["trace"];
child: (bindings: Record<string, any>) => Logger;

@@ -30,0 +30,0 @@ setContext: (context: Record<string, any>) => Logger;

@@ -45,20 +45,74 @@ "use strict";

exports.logger = {
fatal: (obj, message, ...args) => {
getGlobalLogger().fatal(obj, message, ...args);
},
error: (obj, message, ...args) => {
getGlobalLogger().error(obj, message, ...args);
},
warn: (obj, message, ...args) => {
getGlobalLogger().warn(obj, message, ...args);
},
info: (obj, message, ...args) => {
getGlobalLogger().info(obj, message, ...args);
},
debug: (obj, message, ...args) => {
getGlobalLogger().debug(obj, message, ...args);
},
trace: (obj, message, ...args) => {
getGlobalLogger().trace(obj, message, ...args);
},
fatal: ((obj, message, ...args) => {
const globalLogger = getGlobalLogger();
if (obj instanceof Error) {
globalLogger.fatal(obj, message, ...args);
}
else if (typeof obj === 'string') {
globalLogger.fatal(obj, message, ...args);
}
else {
globalLogger.fatal(obj, message, ...args);
}
}),
error: ((obj, message, ...args) => {
const globalLogger = getGlobalLogger();
if (obj instanceof Error) {
globalLogger.error(obj, message, ...args);
}
else if (typeof obj === 'string') {
globalLogger.error(obj, message, ...args);
}
else {
globalLogger.error(obj, message, ...args);
}
}),
warn: ((obj, message, ...args) => {
const globalLogger = getGlobalLogger();
if (obj instanceof Error) {
globalLogger.warn(obj, message, ...args);
}
else if (typeof obj === 'string') {
globalLogger.warn(obj, message, ...args);
}
else {
globalLogger.warn(obj, message, ...args);
}
}),
info: ((obj, message, ...args) => {
const globalLogger = getGlobalLogger();
if (obj instanceof Error) {
globalLogger.info(obj, message, ...args);
}
else if (typeof obj === 'string') {
globalLogger.info(obj, message, ...args);
}
else {
globalLogger.info(obj, message, ...args);
}
}),
debug: ((obj, message, ...args) => {
const globalLogger = getGlobalLogger();
if (obj instanceof Error) {
globalLogger.debug(obj, message, ...args);
}
else if (typeof obj === 'string') {
globalLogger.debug(obj, message, ...args);
}
else {
globalLogger.debug(obj, message, ...args);
}
}),
trace: ((obj, message, ...args) => {
const globalLogger = getGlobalLogger();
if (obj instanceof Error) {
globalLogger.trace(obj, message, ...args);
}
else if (typeof obj === 'string') {
globalLogger.trace(obj, message, ...args);
}
else {
globalLogger.trace(obj, message, ...args);
}
}),
child: (bindings) => {

@@ -65,0 +119,0 @@ return getGlobalLogger().child(bindings);

@@ -42,24 +42,9 @@ "use strict";

if (typeof obj === 'string') {
// logger.info('message') 或 logger.info('message', error/data, ...)
// logger.info('message') 或 logger.info('message', data, ...)
logMessage = obj;
// 检查后续参数中是否有 Error 对象或其他数据
// 检查后续参数中是否有普通对象数据(不处理Error对象)
if (message !== undefined || args.length > 0) {
const allArgs = [message, ...args].filter(arg => arg !== undefined);
for (const arg of allArgs) {
if (arg instanceof Error) {
// 如果是 Error 对象,使用 err 键
if (!logObj.err) {
logObj.err = arg;
}
else {
// 如果已经有错误,创建数组
if (Array.isArray(logObj.err)) {
logObj.err.push(arg);
}
else {
logObj.err = [logObj.err, arg];
}
}
}
else if (typeof arg === 'object' && arg !== null) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
// 如果是普通对象,合并到日志对象中

@@ -71,43 +56,9 @@ Object.assign(logObj, arg);

}
else if (obj instanceof Error) {
// logger.info(error, 'message') - Error 在前,消息在后
logObj.err = obj;
logMessage = message;
// 处理额外参数
for (const arg of args) {
if (arg instanceof Error && arg !== obj) {
// 如果有多个 Error,使用数组
if (Array.isArray(logObj.err)) {
logObj.err.push(arg);
}
else {
logObj.err = [logObj.err, arg];
}
}
else if (typeof arg === 'object' && arg !== null) {
Object.assign(logObj, arg);
}
}
}
else if (obj && typeof obj === 'object') {
else if (obj && typeof obj === 'object' && !(obj instanceof Error)) {
// logger.info({ key: 'value' }, 'message') - 数据在前,消息在后
Object.assign(logObj, obj);
logMessage = message;
// 处理额外参数中的 Error 对象
// 处理额外参数中的普通对象
for (const arg of args) {
if (arg instanceof Error) {
if (!logObj.err) {
logObj.err = arg;
}
else {
// 如果已经有错误,创建数组
if (Array.isArray(logObj.err)) {
logObj.err.push(arg);
}
else {
logObj.err = [logObj.err, arg];
}
}
}
else if (typeof arg === 'object' && arg !== null) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
Object.assign(logObj, arg);

@@ -118,21 +69,7 @@ }

else {
// logger.info() - 没有参数,或者 obj 是其他类型
// logger.info() - 没有参数,或者 obj 是其他类型(包括Error)
logMessage = message;
// 处理额外参数
// 处理额外参数中的普通对象
for (const arg of args) {
if (arg instanceof Error) {
if (!logObj.err) {
logObj.err = arg;
}
else {
// 如果已经有错误,创建数组
if (Array.isArray(logObj.err)) {
logObj.err.push(arg);
}
else {
logObj.err = [logObj.err, arg];
}
}
}
else if (typeof arg === 'object' && arg !== null) {
if (typeof arg === 'object' && arg !== null && !(arg instanceof Error)) {
Object.assign(logObj, arg);

@@ -149,4 +86,17 @@ }

error(obj, message, ...args) {
const [logObj, msg] = this.buildLogObject(obj, message, ...args);
this._pino.error(logObj, msg);
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);
}
}

@@ -153,0 +103,0 @@ warn(obj, message, ...args) {

@@ -28,3 +28,2 @@ import * as stdSerializers from 'pino-std-serializers';

res: typeof stdSerializers.res;
err: (err: Error) => any;
user: (user: any) => any;

@@ -31,0 +30,0 @@ query: (query: any) => any;

@@ -128,7 +128,6 @@ "use strict";

return {
// 只使用 pino 标准的请求和响应序列化器
// 使用 pino 标准的序列化器
req: stdSerializers.req,
res: stdSerializers.res,
// 使用我们自定义的错误序列化器
err: exports.errorSerializer,
// 不使用任何错误序列化器,让pino使用默认处理
// 自定义序列化器

@@ -135,0 +134,0 @@ user: exports.userSerializer,

{
"name": "@hestjs/logger",
"version": "0.1.4",
"version": "0.1.5",
"description": "HestJS Logger - A powerful logging solution based on Pino",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet