@nocobase/logger
Advanced tools
Comparing version 0.18.0-alpha.8 to 0.18.0-alpha.9
export declare const getLoggerLevel: () => string; | ||
export declare const getLoggerFilePath: (...paths: string[]) => string; | ||
export declare const getLoggerTransport: () => ('console' | 'file' | 'dailyRotateFile')[]; | ||
export declare const getLoggerFormat: () => 'logfmt' | 'json' | 'delimiter'; | ||
export declare const getLoggerFormat: () => 'logfmt' | 'json' | 'delimiter' | 'console'; |
@@ -43,3 +43,3 @@ var __create = Object.create; | ||
const getLoggerTransport = /* @__PURE__ */ __name(() => (process.env.LOGGER_TRANSPORT || (process.env.APP_ENV === "development" ? "console" : "console,dailyRotateFile")).split(","), "getLoggerTransport"); | ||
const getLoggerFormat = /* @__PURE__ */ __name(() => process.env.LOGGER_FORMAT || (process.env.APP_ENV === "development" ? "logfmt" : "json"), "getLoggerFormat"); | ||
const getLoggerFormat = /* @__PURE__ */ __name(() => process.env.LOGGER_FORMAT || (process.env.APP_ENV === "development" ? "console" : "json"), "getLoggerFormat"); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -46,0 +46,0 @@ 0 && (module.exports = { |
@@ -7,4 +7,5 @@ import winston from 'winston'; | ||
export declare const logfmtFormat: winston.Logform.Format; | ||
export declare const consoleFormat: winston.Logform.Format; | ||
export declare const delimiterFormat: winston.Logform.Format; | ||
export declare const escapeFormat: winston.Logform.Format; | ||
export declare const sortFormat: winston.Logform.Format; |
@@ -32,2 +32,3 @@ var __create = Object.create; | ||
colorFormat: () => colorFormat, | ||
consoleFormat: () => consoleFormat, | ||
delimiterFormat: () => delimiterFormat, | ||
@@ -44,9 +45,5 @@ escapeFormat: () => escapeFormat, | ||
var import_config = require("./config"); | ||
var import_lodash = require("lodash"); | ||
const DEFAULT_DELIMITER = "|"; | ||
const colorize = { | ||
errors: import_chalk.default.red, | ||
module: import_chalk.default.cyan, | ||
reqId: import_chalk.default.gray, | ||
request: import_chalk.default.green | ||
}; | ||
const colorize = {}; | ||
const getFormat = /* @__PURE__ */ __name((format) => { | ||
@@ -56,2 +53,5 @@ const configFormat = format || (0, import_config.getLoggerFormat)(); | ||
switch (configFormat) { | ||
case "console": | ||
logFormat = import_winston.default.format.combine(consoleFormat); | ||
break; | ||
case "logfmt": | ||
@@ -64,3 +64,3 @@ logFormat = logfmtFormat; | ||
case "json": | ||
logFormat = import_winston.default.format.combine(stripColorFormat, import_winston.default.format.json({ deterministic: false })); | ||
logFormat = import_winston.default.format.combine(import_winston.default.format.json({ deterministic: false })); | ||
break; | ||
@@ -74,14 +74,12 @@ default: | ||
Object.entries(info).forEach(([k, v]) => { | ||
if (k === "message" && info["level"].includes("error")) { | ||
info[k] = colorize.errors(v); | ||
var _a; | ||
const level = info["level"]; | ||
if (colorize[k]) { | ||
info[k] = colorize[k](v); | ||
return; | ||
} | ||
if (k === "reqId" && v) { | ||
info[k] = colorize.reqId(v); | ||
if ((_a = colorize[level]) == null ? void 0 : _a[k]) { | ||
info[k] = colorize[level][k](v); | ||
return; | ||
} | ||
if ((k === "module" || k === "submodule") && v) { | ||
info[k] = colorize.module(v); | ||
} | ||
if (v === "request" || v === "response") { | ||
info[k] = colorize.request(v); | ||
} | ||
}); | ||
@@ -115,2 +113,32 @@ return info; | ||
); | ||
const consoleFormat = import_winston.default.format.printf((info) => { | ||
const keys = ["level", "timestamp", "message"]; | ||
Object.entries(info).forEach(([k, v]) => { | ||
if (typeof v === "object") { | ||
if ((0, import_lodash.isEmpty)(v)) { | ||
info[k] = ""; | ||
return; | ||
} | ||
try { | ||
info[k] = JSON.stringify(v); | ||
} catch (error) { | ||
info[k] = String(v); | ||
} | ||
} | ||
if (v === void 0 || v === null) { | ||
info[k] = ""; | ||
} | ||
}); | ||
const tags = Object.entries(info).filter(([k, v]) => !keys.includes(k) && v).map(([k, v]) => `${k}=${v}`).join(" "); | ||
const level = info.level.padEnd(5, " "); | ||
const message = info.message.padEnd(44, " "); | ||
const color = { | ||
error: import_chalk.default.red, | ||
warn: import_chalk.default.yellow, | ||
info: import_chalk.default.green, | ||
debug: import_chalk.default.blue | ||
}[info.level] || import_chalk.default.white; | ||
const colorized = message.startsWith("Executing") ? color(`${info.timestamp} [${level}]`) + ` ${message}` : color(`${info.timestamp} [${level}] ${message}`); | ||
return `${colorized} ${tags}`; | ||
}); | ||
const delimiterFormat = import_winston.default.format.printf( | ||
@@ -136,6 +164,7 @@ (info) => Object.entries(info).map(([, v]) => { | ||
})(); | ||
const sortFormat = import_winston.default.format((info) => ({ level: info.level, timestamp: info.timestamp, ...info }))(); | ||
const sortFormat = import_winston.default.format((info) => ({ level: info.level, ...info }))(); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
colorFormat, | ||
consoleFormat, | ||
delimiterFormat, | ||
@@ -142,0 +171,0 @@ escapeFormat, |
@@ -5,3 +5,2 @@ export * from './config'; | ||
export * from './request-logger'; | ||
export * from './app-logger'; | ||
export * from './transports'; |
@@ -21,3 +21,2 @@ var __defProp = Object.defineProperty; | ||
__reExport(src_exports, require("./request-logger"), module.exports); | ||
__reExport(src_exports, require("./app-logger"), module.exports); | ||
__reExport(src_exports, require("./transports"), module.exports); | ||
@@ -30,4 +29,3 @@ // Annotate the CommonJS export names for ESM import in node: | ||
...require("./request-logger"), | ||
...require("./app-logger"), | ||
...require("./transports") | ||
}); |
import winston, { Logger } from 'winston'; | ||
import { SystemLoggerOptions } from './system-logger'; | ||
import 'winston-daily-rotate-file'; | ||
@@ -7,3 +6,3 @@ interface LoggerOptions extends Omit<winston.LoggerOptions, 'transports' | 'format'> { | ||
filename?: string; | ||
format?: 'logfmt' | 'json' | 'delimiter' | winston.Logform.Format; | ||
format?: 'logfmt' | 'json' | 'delimiter' | 'console' | winston.Logform.Format; | ||
transports?: ('console' | 'file' | 'dailyRotateFile' | winston.transport)[]; | ||
@@ -14,10 +13,1 @@ } | ||
export { Logger, LoggerOptions }; | ||
interface ReqeustLoggerOptions extends LoggerOptions { | ||
skip?: (ctx?: any) => Promise<boolean>; | ||
requestWhitelist?: string[]; | ||
responseWhitelist?: string[]; | ||
} | ||
export interface AppLoggerOptions { | ||
request: ReqeustLoggerOptions; | ||
system: SystemLoggerOptions; | ||
} |
@@ -61,3 +61,3 @@ var __create = Object.create; | ||
}), | ||
format || import_winston.default.format.combine(import_format.sortFormat, import_format.colorFormat, import_format.logfmtFormat) | ||
format || import_format.consoleFormat | ||
), | ||
@@ -64,0 +64,0 @@ ...rest || {}, |
@@ -1,2 +0,7 @@ | ||
import { AppLoggerOptions } from './logger'; | ||
export declare const requestLogger: (appName: string, options?: AppLoggerOptions) => (ctx: any, next: any) => Promise<void>; | ||
import { LoggerOptions } from './logger'; | ||
export interface RequestLoggerOptions extends LoggerOptions { | ||
skip?: (ctx?: any) => Promise<boolean>; | ||
requestWhitelist?: string[]; | ||
responseWhitelist?: string[]; | ||
} | ||
export declare const requestLogger: (appName: string, options?: RequestLoggerOptions) => (ctx: any, next: any) => Promise<void>; |
@@ -40,6 +40,6 @@ var __defProp = Object.defineProperty; | ||
filename: "request", | ||
...(options == null ? void 0 : options.request) || {} | ||
...options || {} | ||
}); | ||
return async (ctx, next) => { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j; | ||
var _a, _b, _c, _d, _e, _f, _g, _h; | ||
const reqId = ctx.reqId; | ||
@@ -55,7 +55,8 @@ const path = /^\/api\/(.+):(.+)/.exec(ctx.path); | ||
requestLogger2.info({ | ||
reqId, | ||
message: "request", | ||
message: `request ${ctx.method} ${ctx.url}`, | ||
...requestInfo, | ||
req: (0, import_lodash.pick)(ctx.request.toJSON(), ((_a = options == null ? void 0 : options.request) == null ? void 0 : _a.requestWhitelist) || defaultRequestWhitelist), | ||
action: (_c = (_b = ctx.action) == null ? void 0 : _b.toJSON) == null ? void 0 : _c.call(_b) | ||
req: (0, import_lodash.pick)(ctx.request.toJSON(), (options == null ? void 0 : options.requestWhitelist) || defaultRequestWhitelist), | ||
action: (_b = (_a = ctx.action) == null ? void 0 : _a.toJSON) == null ? void 0 : _b.call(_a), | ||
app: appName, | ||
reqId | ||
}); | ||
@@ -71,15 +72,16 @@ let error; | ||
const info = { | ||
reqId, | ||
message: "response", | ||
message: `response ${ctx.url}`, | ||
...requestInfo, | ||
res: (0, import_lodash.pick)(ctx.response.toJSON(), ((_d = options == null ? void 0 : options.request) == null ? void 0 : _d.responseWhitelist) || defaultResponseWhitelist), | ||
action: (_f = (_e = ctx.action) == null ? void 0 : _e.toJSON) == null ? void 0 : _f.call(_e), | ||
userId: (_h = (_g = ctx.auth) == null ? void 0 : _g.user) == null ? void 0 : _h.id, | ||
res: (0, import_lodash.pick)(ctx.response.toJSON(), (options == null ? void 0 : options.responseWhitelist) || defaultResponseWhitelist), | ||
action: (_d = (_c = ctx.action) == null ? void 0 : _c.toJSON) == null ? void 0 : _d.call(_c), | ||
userId: (_f = (_e = ctx.auth) == null ? void 0 : _e.user) == null ? void 0 : _f.id, | ||
status: ctx.status, | ||
cost | ||
cost, | ||
app: appName, | ||
reqId | ||
}; | ||
if (Math.floor(status / 100) == 5) { | ||
requestLogger2.error({ ...info, res: ((_i = ctx.body) == null ? void 0 : _i["errors"]) || ctx.body }); | ||
requestLogger2.error({ ...info, res: ((_g = ctx.body) == null ? void 0 : _g["errors"]) || ctx.body }); | ||
} else if (Math.floor(status / 100) == 4) { | ||
requestLogger2.warn({ ...info, res: ((_j = ctx.body) == null ? void 0 : _j["errors"]) || ctx.body }); | ||
requestLogger2.warn({ ...info, res: ((_h = ctx.body) == null ? void 0 : _h["errors"]) || ctx.body }); | ||
} else { | ||
@@ -86,0 +88,0 @@ requestLogger2.info(info); |
@@ -1,2 +0,2 @@ | ||
import winston from 'winston'; | ||
import { Logger } from 'winston'; | ||
import { LoggerOptions } from './logger'; | ||
@@ -6,2 +6,14 @@ export interface SystemLoggerOptions extends LoggerOptions { | ||
} | ||
export declare const createSystemLogger: (options: SystemLoggerOptions) => winston.Logger; | ||
export type logMethod = (message: string, meta?: { | ||
module?: string; | ||
submodule?: string; | ||
method?: string; | ||
[key: string]: any; | ||
}) => SystemLogger; | ||
export interface SystemLogger extends Omit<Logger, 'info' | 'warn' | 'error' | 'debug'> { | ||
info: logMethod; | ||
warn: logMethod; | ||
error: logMethod; | ||
debug: logMethod; | ||
} | ||
export declare const createSystemLogger: (options: SystemLoggerOptions) => SystemLogger; |
@@ -61,3 +61,3 @@ var __create = Object.create; | ||
log(info, callback) { | ||
const { level, message, reqId, [import_triple_beam.SPLAT]: args } = info; | ||
const { level, message, reqId, app, [import_triple_beam.SPLAT]: args } = info; | ||
const logger = level === "error" && this.errorLogger ? this.errorLogger : this.logger; | ||
@@ -67,8 +67,9 @@ const { module: module2, submodule, method, ...meta } = (args == null ? void 0 : args[0]) || {}; | ||
level, | ||
reqId, | ||
message, | ||
meta, | ||
module: module2 || info["module"] || "", | ||
submodule: submodule || info["submodule"] || "", | ||
method: method || "", | ||
meta | ||
app, | ||
reqId | ||
}); | ||
@@ -75,0 +76,0 @@ callback(null, true); |
@@ -67,3 +67,3 @@ var __create = Object.create; | ||
console: () => Transports.console({ | ||
format: import_winston.default.format.combine(import_winston.default.format.colorize(), import_format.colorFormat, format) | ||
format: import_winston.default.format.combine(format) | ||
}), | ||
@@ -70,0 +70,0 @@ file: () => Transports.file({ |
{ | ||
"name": "@nocobase/logger", | ||
"version": "0.18.0-alpha.8", | ||
"version": "0.18.0-alpha.9", | ||
"description": "nocobase logging library", | ||
@@ -21,3 +21,3 @@ "license": "Apache-2.0", | ||
}, | ||
"gitHead": "727d42f6f14e5f863831da3dbf3255ba1165b567" | ||
"gitHead": "34ca0df4eede2e83fc86297b0fe19eba970e2b1b" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47910
20
875