Skip to content

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

@phuze

Description

@phuze

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 my-app@1.0.0~start: Failed to exec start script
[ERROR]  npm ERR! code ELIFECYCLE
[ERROR]  npm ERR! errno 1
[ERROR]  npm ERR! my-app@1.0.0 start: `node server.js`
[ERROR]  npm ERR! Exit status 1
[ERROR]  npm ERR!
[ERROR]  npm ERR! Failed at the my-app@1.0.0 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions