Firebase features: logging with winston in packdriver console
I cannot do winston logger to write logs to stackdriver . I am deploying my functions as google firebase functions (using firebase deploy
). console
works well, but we are not using this tool in the project.
What I have 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!');
as well as adding with parameters such as project idprojectId
/ JSON service accountkeyFilename
); - using https://github.com/findanyemail/winston-transport-stackdriver-error-reporting . Also out of luck. I still can't see the logs on the stack.
Please suggest ... I'm tired of experimenting (every redeploy takes time)
+3
source to share
1 answer
Finally, what I did was implement a custom transport that actually calls console.log
under the hood. That 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);
};
+1
source to share