8

我的公司有一个定制开发的 logger 包,我们希望将它用作 fastify 中的默认 logger。我试图通过下面这个简单的示例来了解如何注册我的记录器,但 fastify 总是使用 Pino。

index.js

const log = require("./Logger");
const fastify = require("fastify")({ logger: log });

fastify.get("/", (request, reply) => {
    request.log(
        "includes request information, but is the same logger instance as `log`"
    );
    reply.send({ hello: "world" });
});

fastify.listen(3000)

logger.js

function Logger(...args) {
    this.args = args;
}
Logger.prototype.info = function(msg) {
    console.log("myLogger", msg);
};

logger.js还包含error, debug, fatal, warn, trace,child函数,但函数体是相同的。

结果是:

{"level":30,"time":1553095994942,"msg":"Server listening at http://127.0.0.1:3000","pid":14543,"hostname":"VirtualBox","v":1}

whitch 是默认的 Pino 输出。

4

2 回答 2

12

here所述,您的记录器

必须有以下方法

所以这个例子有效:

function Logger(...args) {
  this.args = args;
}
Logger.prototype.info = function (msg) { console.log("myLogger", msg); };
Logger.prototype.error = function (msg) { console.log("myLogger", msg); };
Logger.prototype.debug = function (msg) { console.log("myLogger", msg); };
Logger.prototype.fatal = function (msg) { console.log("myLogger", msg); };
Logger.prototype.warn = function (msg) { console.log("myLogger", msg); };
Logger.prototype.trace = function (msg) { console.log("myLogger", msg); };
Logger.prototype.child = function () { return new Logger() };


const myLogger = new Logger()
const app = require('fastify')({
  logger: myLogger
})

app.get("/", (request, reply) => {
  request.log.info('hi');
  reply.send({ hello: "world" });
});

app.listen(3000)

在这里您可以检查应用于您的参数的记录器验证

于 2019-03-20T16:46:35.390 回答
0

如果您需要 log4js,这是一个支持 json 事件的自定义记录器。你c

import log4js from "log4js";

import {log4jsConfig} from "./log4js.config.js";

log4js.addLayout('json', function (config) {
    return function (logEvent) {
        logEvent.application = 'my-app';
        logEvent.processId = process.pid;
        if (process.env.IP) {
            logEvent.ip = process.env.IP;
        }
        if (process.env.ENV) {
            logEvent.ENV = process.env.ENV;
        }
        if (logEvent.data && logEvent.data.length > 0) {
            logEvent.message = logEvent.data[0];
            delete logEvent.data;
        }
        return JSON.stringify(logEvent);
    };
});
const log = log4js.getLogger('engine');

log4js.configure(log4jsConfig);

export class AndromedaLogger {
    logger;
    loggerOptions;

    constructor(args) {
        this.logger = log;
    }

    get Logger() {
        return this.logger;
    }

    static configGlobal(options) {
        this.loggerOptions = options;
    }

    info(message) {
        this.logger.info(message);
    }

    error(message, trace) {
        this.logger.error(`${message} -> (${trace || 'trace not provided !'})`);
    }

    warn(message) {
        this.logger.warn(message);
    }

    debug(message, context) {
        this.logger.debug(message);
    }

    trace(message, context) {
        this.logger.trace(message);
    }

    fatal(message, context) {
        this.logger.fatal(message);
    }

    child() {
        return new AndromedaLogger()
    };


}

log4js 配置:

const _log4jsConfig = {
  appenders: {},
  categories: {
    default: {
      appenders: [],
      level: 'trace',
    },
  },
};

_log4jsConfig.appenders.stdout = {
  type: 'stdout',
  layout: { type: 'colored' },
};

_log4jsConfig.appenders['file'] = {
  type: 'file',
  filename: 'logs/app.log',
  maxLogSize: 104857600,
  numBackups: 3,
};

_log4jsConfig.categories.default.appenders.push('stdout');
_log4jsConfig.categories.default.appenders.push('file');

export const log4jsConfig = _log4jsConfig;
于 2021-12-24T22:16:24.367 回答