![](/img/trans.png)
[英]How to add custom function to Winston logger in Typescript?
[英]How to add custom data to the `info` object using a Winston custom transport?
我目前正在為 Winston 制作自定義傳輸,我需要將元數據添加到info
object,如下所示:
{
message: 'Test',
level: 'warn',
timestamp: '2022-12-02T17:27:14.420Z',
event_id: 'test',
[Symbol(level)]: 'warn',
[Symbol(message)]: '\x1B[33m[warn] 2022-12-02T17:27:14.420Z : Test\x1B[39m'
}
這是我在代碼中使用console.log(info)
時應該出現的內容。 但是,我遇到了一個非常奇怪的問題,即幾乎似乎是“遞歸”堆棧跟蹤。 我把它縮短了一些,這樣更容易理解。
// from `console.log(info)`
message: 'Test',
level: 'warn',
timestamp: '2022-12-02T17:29:26.007Z',
[Symbol(level)]: 'warn'
}
// the warning I made
[warn] 2022-12-02T17:29:26.007Z : Test
// Nothing after here should happen!
Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({
message: 'Test',
level: 'warn',
timestamp: '2022-12-02T17:29:26.007Z',
event_id: 'test',
[Symbol(level)]: 'warn',
[Symbol(message)]: '\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\x1B[39m'
})
at new NodeError (node:internal/errors:393:5)
at DerivedLogger.emit (node:events:502:17)
at DerivedLogger.emit (node:domain:489:12)
at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)
at CustomTransport.emit (node:events:525:35)
at CustomTransport.emit (node:domain:489:12)
at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)
at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)
at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)
at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5)
{
error: Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({
message: 'Test',
level: 'warn',
timestamp: '2022-12-02T17:29:26.007Z',
event_id: 'test',
[Symbol(level)]: 'warn',
[Symbol(message)]: '\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\x1B[39m'
})
at new NodeError (node:internal/errors:393:5)
at DerivedLogger.emit (node:events:502:17)
at DerivedLogger.emit (node:domain:489:12)
at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)
at CustomTransport.emit (node:events:525:35)
at CustomTransport.emit (node:domain:489:12)
at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)
at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)
at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)
at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5) {
code: 'ERR_UNHANDLED_ERROR',
context: {
message: 'Test',
level: 'warn',
timestamp: '2022-12-02T17:29:26.007Z',
event_id: 'test',
[Symbol(level)]: 'warn',
[Symbol(message)]: '\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\x1B[39m'
}
},
level: 'error',
message: 'uncaughtException: Unhandled error. ({\n' +
" message: 'Test',\n" +
" level: 'warn',\n" +
" timestamp: '2022-12-02T17:29:26.007Z',\n" +
" event_id: 'test',\n" +
" [Symbol(level)]: 'warn',\n" +
" [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\n" +
'})\n' +
'Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({\n' +
" message: 'Test',\n" +
" level: 'warn',\n" +
" timestamp: '2022-12-02T17:29:26.007Z',\n" +
" event_id: 'test',\n" +
" [Symbol(level)]: 'warn',\n" +
" [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\n" +
'})\n' +
' at new NodeError (node:internal/errors:393:5)\n' +
' at DerivedLogger.emit (node:events:502:17)\n' +
' at DerivedLogger.emit (node:domain:489:12)\n' +
' at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)\n' +
' at CustomTransport.emit (node:events:525:35)\n' +
' at CustomTransport.emit (node:domain:489:12)\n' +
' at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)\n' +
' at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)\n' +
' at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)\n' +
' at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5)',
stack: 'Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({\n' +
" message: 'Test',\n" +
" level: 'warn',\n" +
" timestamp: '2022-12-02T17:29:26.007Z',\n" +
" event_id: 'test',\n" +
" [Symbol(level)]: 'warn',\n" +
" [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\n" +
'})\n' +
' at new NodeError (node:internal/errors:393:5)\n' +
' at DerivedLogger.emit (node:events:502:17)\n' +
' at DerivedLogger.emit (node:domain:489:12)\n' +
' at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)\n' +
' at CustomTransport.emit (node:events:525:35)\n' +
' at CustomTransport.emit (node:domain:489:12)\n' +
' at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)\n' +
' at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)\n' +
' at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)\n' +
' at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5)',
exception: true,
date: 'Fri Dec 02 2022 12:29:26 GMT-0500 (Eastern Standard Time)',
process: {
pid: 18296,
uid: 1000,
gid: 1000,
cwd: '/home/ethan/Documents/StatusPlusDiscordBot',
execPath: '/home/ethan/.nvm/versions/node/v18.10.0/bin/node',
version: 'v18.10.0',
argv: [
'/home/ethan/.nvm/versions/node/v18.10.0/bin/node',
'/home/ethan/Documents/StatusPlusDiscordBot/src/index.js',
'src/index.js'
],
memoryUsage: {
rss: 91942912,
heapTotal: 59322368,
heapUsed: 29791992,
external: 20277528,
arrayBuffers: 18684631
}
},
os: { loadavg: [ 0.88, 0.95, 0.82 ], uptime: 4224.99 },
trace: [
{
column: 5,
file: 'node:internal/errors',
function: 'new NodeError',
line: 393,
method: null,
native: false
},
{
column: 17,
file: 'node:events',
function: 'DerivedLogger.emit',
line: 502,
method: 'emit',
native: false
},
{
column: 12,
file: 'node:domain',
function: 'DerivedLogger.emit',
line: 489,
method: 'emit',
native: false
},
{
column: 12,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js',
function: 'DerivedLogger.transportEvent',
line: 639,
method: 'transportEvent',
native: false
},
{
column: 35,
file: 'node:events',
function: 'CustomTransport.emit',
line: 525,
method: 'emit',
native: false
},
{
column: 12,
file: 'node:domain',
function: 'CustomTransport.emit',
line: 489,
method: 'emit',
native: false
},
{
column: 101,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js',
function: 'errorOrDestroy',
line: 98,
method: null,
native: false
},
{
column: 5,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js',
function: 'onwriteError',
line: 424,
method: null,
native: false
},
{
column: 11,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js',
function: 'onwrite',
line: 450,
method: null,
native: false
},
{
column: 5,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js',
function: 'WritableState.onwrite',
line: 160,
method: 'onwrite',
native: false
}
],
timestamp: '2022-12-02T17:29:26.044Z',
[Symbol(level)]: 'error'
}
{
error: Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({
message: 'Test',
level: 'warn',
timestamp: '2022-12-02T17:29:26.007Z',
event_id: 'test',
[Symbol(level)]: 'warn',
[Symbol(message)]: '\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\x1B[39m'
})
at new NodeError (node:internal/errors:393:5)
at DerivedLogger.emit (node:events:502:17)
at DerivedLogger.emit (node:domain:489:12)
at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)
at CustomTransport.emit (node:events:525:35)
at CustomTransport.emit (node:domain:489:12)
at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)
at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)
at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)
at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5) {
code: 'ERR_UNHANDLED_ERROR',
context: {
message: 'Test',
level: 'warn',
timestamp: '2022-12-02T17:29:26.007Z',
event_id: 'test',
[Symbol(level)]: 'warn',
[Symbol(message)]: '\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\x1B[39m'
}
},
level: 'error',
message: 'uncaughtException: Unhandled error. ({\n' +
" message: 'Test',\n" +
" level: 'warn',\n" +
" timestamp: '2022-12-02T17:29:26.007Z',\n" +
" event_id: 'test',\n" +
" [Symbol(level)]: 'warn',\n" +
" [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\n" +
'})\n' +
'Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({\n' +
" message: 'Test',\n" +
" level: 'warn',\n" +
" timestamp: '2022-12-02T17:29:26.007Z',\n" +
" event_id: 'test',\n" +
" [Symbol(level)]: 'warn',\n" +
" [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\n" +
'})\n' +
' at new NodeError (node:internal/errors:393:5)\n' +
' at DerivedLogger.emit (node:events:502:17)\n' +
' at DerivedLogger.emit (node:domain:489:12)\n' +
' at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)\n' +
' at CustomTransport.emit (node:events:525:35)\n' +
' at CustomTransport.emit (node:domain:489:12)\n' +
' at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)\n' +
' at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)\n' +
' at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)\n' +
' at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5)',
stack: 'Error [ERR_UNHANDLED_ERROR]: Unhandled error. ({\n' +
" message: 'Test',\n" +
" level: 'warn',\n" +
" timestamp: '2022-12-02T17:29:26.007Z',\n" +
" event_id: 'test',\n" +
" [Symbol(level)]: 'warn',\n" +
" [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\n" +
'})\n' +
' at new NodeError (node:internal/errors:393:5)\n' +
' at DerivedLogger.emit (node:events:502:17)\n' +
' at DerivedLogger.emit (node:domain:489:12)\n' +
' at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)\n' +
' at CustomTransport.emit (node:events:525:35)\n' +
' at CustomTransport.emit (node:domain:489:12)\n' +
' at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)\n' +
' at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)\n' +
' at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)\n' +
' at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5)',
exception: true,
date: 'Fri Dec 02 2022 12:29:26 GMT-0500 (Eastern Standard Time)',
process: {
pid: 18296,
uid: 1000,
gid: 1000,
cwd: '/home/ethan/Documents/StatusPlusDiscordBot',
execPath: '/home/ethan/.nvm/versions/node/v18.10.0/bin/node',
version: 'v18.10.0',
argv: [
'/home/ethan/.nvm/versions/node/v18.10.0/bin/node',
'/home/ethan/Documents/StatusPlusDiscordBot/src/index.js',
'src/index.js'
],
memoryUsage: {
rss: 91942912,
heapTotal: 59322368,
heapUsed: 29791992,
external: 20277528,
arrayBuffers: 18684631
}
},
os: { loadavg: [ 0.88, 0.95, 0.82 ], uptime: 4224.99 },
trace: [
{
column: 5,
file: 'node:internal/errors',
function: 'new NodeError',
line: 393,
method: null,
native: false
},
{
column: 17,
file: 'node:events',
function: 'DerivedLogger.emit',
line: 502,
method: 'emit',
native: false
},
{
column: 12,
file: 'node:domain',
function: 'DerivedLogger.emit',
line: 489,
method: 'emit',
native: false
},
{
column: 12,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js',
function: 'DerivedLogger.transportEvent',
line: 639,
method: 'transportEvent',
native: false
},
{
column: 35,
file: 'node:events',
function: 'CustomTransport.emit',
line: 525,
method: 'emit',
native: false
},
{
column: 12,
file: 'node:domain',
function: 'CustomTransport.emit',
line: 489,
method: 'emit',
native: false
},
{
column: 101,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js',
function: 'errorOrDestroy',
line: 98,
method: null,
native: false
},
{
column: 5,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js',
function: 'onwriteError',
line: 424,
method: null,
native: false
},
{
column: 11,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js',
function: 'onwrite',
line: 450,
method: null,
native: false
},
{
column: 5,
file: '/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js',
function: 'WritableState.onwrite',
line: 160,
method: 'onwrite',
native: false
}
],
timestamp: '2022-12-02T17:29:26.044Z',
[Symbol(level)]: 'error',
[Symbol(message)]: '\x1B[31m[error] 2022-12-02T17:29:26.044Z: uncaughtException: Unhandled error. ({\x1B[39m\n' +
"\x1B[31m message: 'Test',\x1B[39m\n" +
"\x1B[31m level: 'warn',\x1B[39m\n" +
"\x1B[31m timestamp: '2022-12-02T17:29:26.007Z',\x1B[39m\n" +
"\x1B[31m event_id: 'test',\x1B[39m\n" +
"\x1B[31m [Symbol(level)]: 'warn',\x1B[39m\n" +
"\x1B[31m [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\x1B[39m\n" +
'\x1B[31m})\x1B[39m\n' +
'\x1B[31mError [ERR_UNHANDLED_ERROR]: Unhandled error. ({\x1B[39m\n' +
"\x1B[31m message: 'Test',\x1B[39m\n" +
"\x1B[31m level: 'warn',\x1B[39m\n" +
"\x1B[31m timestamp: '2022-12-02T17:29:26.007Z',\x1B[39m\n" +
"\x1B[31m event_id: 'test',\x1B[39m\n" +
"\x1B[31m [Symbol(level)]: 'warn',\x1B[39m\n" +
"\x1B[31m [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\x1B[39m\n" +
'\x1B[31m})\x1B[39m\n' +
'\x1B[31m at new NodeError (node:internal/errors:393:5)\x1B[39m\n' +
'\x1B[31m at DerivedLogger.emit (node:events:502:17)\x1B[39m\n' +
'\x1B[31m at DerivedLogger.emit (node:domain:489:12)\x1B[39m\n' +
'\x1B[31m at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)\x1B[39m\n' +
'\x1B[31m at CustomTransport.emit (node:events:525:35)\x1B[39m\n' +
'\x1B[31m at CustomTransport.emit (node:domain:489:12)\x1B[39m\n' +
'\x1B[31m at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)\x1B[39m\n' +
'\x1B[31m at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)\x1B[39m\n' +
'\x1B[31m at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)\x1B[39m\n' +
'\x1B[31m at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:160:5)\x1B[39m\n' +
'\x1B[31mError [ERR_UNHANDLED_ERROR]: Unhandled error. ({\x1B[39m\n' +
"\x1B[31m message: 'Test',\x1B[39m\n" +
"\x1B[31m level: 'warn',\x1B[39m\n" +
"\x1B[31m timestamp: '2022-12-02T17:29:26.007Z',\x1B[39m\n" +
"\x1B[31m event_id: 'test',\x1B[39m\n" +
"\x1B[31m [Symbol(level)]: 'warn',\x1B[39m\n" +
"\x1B[31m [Symbol(message)]: '\\x1B[33m[warn] 2022-12-02T17:29:26.007Z : Test\\x1B[39m'\x1B[39m\n" +
'\x1B[31m})\x1B[39m\n' +
'\x1B[31m at new NodeError (node:internal/errors:393:5)\x1B[39m\n' +
'\x1B[31m at DerivedLogger.emit (node:events:502:17)\x1B[39m\n' +
'\x1B[31m at DerivedLogger.emit (node:domain:489:12)\x1B[39m\n' +
'\x1B[31m at DerivedLogger.transportEvent (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/winston/lib/winston/logger.js:639:12)\x1B[39m\n' +
'\x1B[31m at CustomTransport.emit (node:events:525:35)\x1B[39m\n' +
'\x1B[31m at CustomTransport.emit (node:domain:489:12)\x1B[39m\n' +
'\x1B[31m at errorOrDestroy (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/internal/streams/destroy.js:98:101)\x1B[39m\n' +
'\x1B[31m at onwriteError (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:424:5)\x1B[39m\n' +
'\x1B[31m at onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/_stream_writable.js:450:11)\x1B[39m\n' +
'\x1B[31m at WritableState.onwrite (/home/ethan/Documents/StatusPlusDiscordBot/node_modules/readable-stream/lib/e.js:160:5)\x1B[39m'
}
不幸的是,缺少 Winston-Transport 文檔,所以我不得不猜測如何在傳輸中添加自定義信息。
我的class如下:
class CustomTransport extends Transport {
// eslint-disable-next-line no-useless-constructor
constructor(opts) {
super(opts);
}
// eslint-disable-next-line class-methods-use-this
log(info, callback) {
info.event_id = 'test'
// this.emit('logged', info);
webhook(info);
callback(info);
}
}
當然,這會導致問題。 從回調中刪除info
也解決了這個問題,但是自定義信息不會添加到主庫中使用的info
object 中。
我應該如何解決這個問題 go? 我做錯了什么嗎?
謝謝!
進行如下所述的更改后,我仍然沒有看到任何額外的信息記錄到控制台(我至少沒有包含任何額外信息)但是“遞歸”堆棧跟蹤消失了。
如果有幫助,這是我用於登錄的完整代碼:
// Full-Scale Logging stack for Status+ - @FxllenCode
// This may be open source some time in the future :D
// This is a modified version of the original logger.js file
// It uses Sentry.io for a full-scale logging stack, but also Winston for local logging, and a webhook for a remote logging stack
const winston = require('winston');
const Sentry = require('@sentry/node')
const Tracing = require('@sentry/tracing');
const Transport = require('winston-transport');
const webhook = require('./hook.js')
// Using LogForm standard, create a nice logging format
const format = winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.printf((info) => {
console.log(info)
if (info.stack) {
return winston.format.colorize().colorize(info.level, `[${info.level}] ${info.timestamp}: ${info.message}\n${info.stack}`);
}
return winston.format.colorize().colorize(info.level, `[${info.level}] ${info.timestamp} : ${info.message}`);
}),
);
class CustomTransport extends Transport {
// eslint-disable-next-line no-useless-constructor
constructor(opts) {
super(opts);
}
// eslint-disable-next-line class-methods-use-this
log(info, callback) {
info.event_id = 'test'
info.custom_data = { key1: 'value1' }
webhook(info);
callback(null, info);
}
}
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
transports: [
new CustomTransport(),
new winston.transports.Console(),
],
format,
});
module.exports = logger;
但是,在查看此處后,您對缺少文檔的看法是正確的:
https://github.com/winstonjs/winston#adding-custom-transports
自定義傳輸中的日志 function 似乎應該接受兩個參數: info
和callback
。 info
參數是一個包含日志消息和相關元數據的 object。 callback
參數是一個 function,調用時應將錯誤 object 或 null 作為第一個參數,並將信息 object 和任何其他數據作為第二個參數。 這應該允許自定義傳輸在將信息 object 傳回主庫之前將其他數據添加到信息中。
現在查看您的代碼:
class CustomTransport extends Transport {
// eslint-disable-next-line no-useless-constructor
constructor(opts) {
super(opts);
}
// eslint-disable-next-line class-methods-use-this
log(info, callback) {
info.event_id = 'test'
// this.emit('logged', info);
webhook(info);
callback(info);
}
}
如果你想在信息object中添加自定義信息,那么你需要調用callback(null, info);
而不是callback(info)
。 這應該將帶有自定義信息的信息 object 傳遞給主庫,它將在信息 object 中可用。
編輯:
下面的代碼應該為 Winston 創建自定義傳輸,這是一個日志庫。
重寫 log() 方法以添加自定義字段“event_id”,其值為“test”以及具有指定值的 custom_data 字段到信息 object。
然后將此信息 object 傳遞給 webhook 方法,並使用更新后的信息 object 調用回調 function。
最后,將自定義傳輸添加到 Winston 記錄器實例,可用於記錄消息。
添加custom_data
字段是為了讓我們可以使用它來添加特定於應用程序的額外數據,或者在本例中用於測試。
例如,如果您正在記錄與在線商店相關的事件,您可以添加有關客戶、他們的訂單或與該事件相關的任何其他數據的信息。 這將允許對應用程序中發生的事件進行更詳細的記錄和分析。
// Create a custom transport for Winston
class CustomTransport extends Transport {
constructor(opts) {
super(opts);
}
log(info, callback) { // Override the log() method
info.event_id = 'test' // Add the event_id field with the value 'test' to the info object
info.custom_data = { key1: 'value1', key2: 'value2' } // Add a custom_data field with the specified values to the info object for further analysis
webhook(info); // Call the webhook method with the updated info object
callback(null, info); // Call the callback function with the updated info object
}
}
// Use the custom transport in your Winston logger
const logger = createLogger({
transports: [new CustomTransport()] // Add the custom transport to the Winston logger instance
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.