winston version?_winston@2winston@3 node -v outputs:_ v8.10Deploying a NodeJS Lambda function that uses Winston Console transport for logging fails silently to deliver log commands to the Lambda output. No errors, just no logs. Winston Console transport prefers console._stdout and console._stderr to console.log() and console.error(), but Lambda does not support console._stdout and console._stderr.
From console.js:
if (console._stdout) {
// Node.js maps `process.stdout` to `console._stdout`.
console._stdout.write(`${info[MESSAGE]}${this.eol}`);
} else {
// console.log adds a newline.
console.log(info[MESSAGE]);
}
The problem I see is that console._stdout and console._stderr exist in the Lambda runtime environment so the checks in Winston do not fall back to console.log(), etc.
I would expect Winston's conditional check to account for the presence of console._stdout and console._stderr _AND_ either a configuration or an environment detail that allows it to skip these methods of logging in AWS Lambda environments as they are unsupported.
I'm able to work around this in my lambda functions by including this _hack_ in my handler file:
delete console['_stdout'];
delete console['_stderr'];
I'd love to be able to keep from modifying the prototype of console, but this works for now.
It seems to work fine for me. Could it be caused by something else as I'm also running my lambdas in Node v8.10 with Winston v3.2.1.
I really have no idea what happened between when I logged this and now, but I went back and removed the delete lines that I needed at the time, and it is working fine now. Magic.
@creativeux did you ever figure out what was going on? I'm having the exact problem that you originally reported and your delete console['_stdout]` trick is the only option I have right now.
_I'm on AWS Lambda Node 8.10 + Winston 3.2.1_
@davemkirk I really wish I had more information for you, but I literally just removed the delete lines and it started working. Are you executing straight ES5/6/7 or are you running ES or TS through a minifier/pre-processor? I spent some time tuning webpack to generate cleanly structured zip files before I tried to remove the deletes... I can't immediately think of a reason that a pre-processor would cause the issue, but it's the only thing I can think of that I did between the issue manifesting and it vanishing.
@davemkirk I managed to get this working using https://github.com/winstonjs/winston/issues/1305#issuecomment-387634897
import winston from 'winston'
const LEVEL = Symbol.for('level')
const MESSAGE = Symbol.for('message')
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.Console({
log(info, callback) {
setImmediate(() => this.emit('logged', info))
if (this.stderrLevels[info[LEVEL]]) {
console.error(info[MESSAGE])
if (callback) callback()
return
}
console.log(info[MESSAGE])
if (callback) callback()
},
}),
],
})
export default logger
Wanted to add a comment just in case someone had the same issue I did:
You must include the json format somewhere in your configuration or it might not work.
What I had originally:
new winston.transports.Console({
format: combine(
timestamp(),
errors({ stack: true })
),
log(info, callback) {
setImmediate(() => this.emit('logged', info));
if (this.stderrLevels[info[LEVEL]]) {
console.error(info[MESSAGE]);
if (callback) {
callback();
}
return;
}
console.log(info[MESSAGE]);
if (callback) {
callback();
}
}
}),
What made it work:
new winston.transports.Console({
format: combine(
timestamp(),
errors({ stack: true }),
json()
),
log(info, callback) {
setImmediate(() => this.emit('logged', info));
if (this.stderrLevels[info[LEVEL]]) {
console.error(info[MESSAGE]);
if (callback) {
callback();
}
return;
}
console.log(info[MESSAGE]);
if (callback) {
callback();
}
}
}),
After 1.5 days of troubleshooting, found that all I'd to do make the logs appear in aws lambda is to pass the logger level in the right character case. I was setting it to INFO instead of info and winston had no alert statements for this misconfig.
Most helpful comment
@davemkirk I managed to get this working using https://github.com/winstonjs/winston/issues/1305#issuecomment-387634897