Firebase functions: logging with winston in stackdriver console

穿精又带淫゛_ 提交于 2019-12-10 16:34:47

问题


I cannot make winston logger to write logs to stackdriver console. I deploy my functions as google firebase functions (using firebase deploy). console logging works fine, but we don't use such tool in the project.

What I tried:

  • output to stderr using https://github.com/greglearns/winston-stderr
  • using https://www.npmjs.com/package/@google-cloud/logging-winston (both winston.add(require('@google-cloud/logging-winston')); winston.log('error', 'Winston error!'); and also adding with parameters such as project ID projectId / service account JSON credentials file keyFilename);
  • using https://github.com/findanyemail/winston-transport-stackdriver-error-reporting . Also no luck. I still cannot see logs in stackdriver.

Please suggest... I'm tired of experiments (each re-deploy takes time)


回答1:


Finally what I did - implemented custom transport which actually calls console.log under the hood. This helped.

const winston = require('winston');
const util = require('util');
const ClassicConsoleLoggerTransport = winston.transports.CustomLogger = function (options) {
    options = options || {};
    this.name = 'ClassicConsoleLoggerTransport';
    this.level = options.level || 'info';
    // Configure your storage backing as you see fit
};
util.inherits(ClassicConsoleLoggerTransport, winston.Transport);

ClassicConsoleLoggerTransport.prototype.log = function (level, msg, meta, callback) {
    let args = [msg, '---', meta];
    switch (level) {
        case 'verbose':
        case 'debug':
            console.log.apply(null, args);
            break;
        case 'notice':
        case 'info':
            console.info.apply(null, args);
            break;
        case 'warn':
        case 'warning':
            console.warn.apply(null, args);
            break;
        case 'error':
        case 'crit':
        case 'alert':
        case 'emerg':
            console.error.apply(null, args);
            break;
        default:
            console.log.apply(null, args);
    }
    callback(null, true);
};



回答2:


Winston's default Console transport fails because it uses console._stdout.write when it's available, which is not accepted by Firebase Functions.

There's now a Google Cloud transport package for Stackdriver you can try. Haven't used it and it requires node ^8.11.2 if you're using Winston 3.



来源:https://stackoverflow.com/questions/45479084/firebase-functions-logging-with-winston-in-stackdriver-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!