Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Winston: "Cannot read property 'levels' of undefined" when Logger created without arguments #117

Open
phuze opened this issue Apr 23, 2022 · 1 comment

Comments

@phuze
Copy link

phuze commented Apr 23, 2022

I have a node-based project running as a linux Azure App Service (with Application Insights enabled).
In my application, when winston is initiated without a config object:

const winston = require('winston')
const logger = winston.createLogger() // fails
module.exports = logger

It fails:

[INFO]    _____
[INFO]    /  _  \ __________ _________   ____
[INFO]   /  /_\  \___   /  |  \_  __ \_/ __ \
[INFO]  /    |    \/    /|  |  /|  | \/\  ___/
[INFO]  \____|__  /_____ \____/ |__|    \___  >
[INFO]          \/      \/                  \/
[INFO]  A P P   S E R V I C E   O N   L I N U X
[INFO]
[INFO]  Documentation: http://aka.ms/webapp-linux
[INFO]  NodeJS quickstart: https://aka.ms/node-qs
[INFO]  NodeJS Version : v16.8.0
[INFO]
[ERROR]  /agents/node/node_modules/diagnostic-channel-publishers/dist/src/winston.pub.js:116
[ERROR]          var levels = arguments[0].levels || originalWinston.config.npm.levels;
[ERROR]                                    ^
[ERROR]
[ERROR]  TypeError: Cannot read property 'levels' of undefined
[ERROR]      at Object.patchedCreate [as createLogger] (/agents/node/node_modules/diagnostic-channel-publishers/dist/src/winston.pub.js:116:35)
[ERROR]      at Object.<anonymous> (/home/site/wwwroot/server/utils/logger.js:19:24)
[ERROR]      at Module._compile (node:internal/modules/cjs/loader:1101:14)
[ERROR]      at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
[ERROR]      at Module.load (node:internal/modules/cjs/loader:981:32)
[ERROR]      at Function.Module._load (node:internal/modules/cjs/loader:822:12)
[ERROR]      at Module.require (node:internal/modules/cjs/loader:1005:19)
[ERROR]      at Module.patchedRequire [as require] (/agents/node/node_modules/diagnostic-channel/dist/src/patchRequire.js:15:46)
[ERROR]      at require (node:internal/modules/cjs/helpers:94:18)
[ERROR]      at Object.<anonymous> (/home/site/wwwroot/server/router/api.js:4:16)
[ERROR]  npm info lifecycle [email protected]~start: Failed to exec start script
[ERROR]  npm ERR! code ELIFECYCLE
[ERROR]  npm ERR! errno 1
[ERROR]  npm ERR! [email protected] start: `node server.js`
[ERROR]  npm ERR! Exit status 1
[ERROR]  npm ERR!
[ERROR]  npm ERR! Failed at the [email protected] start script.
[ERROR]  npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

Even though passing nothing is considered valid by Winston: https://github.com/winstonjs/winston/blob/master/index.d.ts#L184

// index.d.ts
let createLogger: (options?: LoggerOptions) => Logger;

Works correctly if empty object {} supplied as argument to createLogger:

const winston = require('winston')
const logger = winston.createLogger({}) // succeeds
module.exports = logger
@phuze phuze changed the title Winston: 'Cannot read property level of undefined' when Logger created without arguments Winston: 'Cannot read property 'levels' of undefined' when Logger created without arguments Apr 23, 2022
@phuze phuze changed the title Winston: 'Cannot read property 'levels' of undefined' when Logger created without arguments Winston: "Cannot read property 'levels' of undefined" when Logger created without arguments Apr 23, 2022
@EJBroeders
Copy link
Contributor

Seems to be related to #115 . I'm in favor of refactoring the Winston patch code a bit more. @microsoftopensource , would it be okay if I publish a PR for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants