The function 'log' is not bound with logger instance, resulting error when calling function 'log' without the logger instance
See original GitHub issueMy problem is, if I’m gonna use function ‘log’ alone, like passing function ‘log’ to some other object, am I suppose to write object.log = logger.log.bind(logger)
instead of object.log = logger.log
, or should winston take care of the binding?
const winston = require('winston');
/* Either way of declaring logger instance is not working
let logger = winston.loggers.get('some_name');
logger.configure({
level: 'info',
transports: [
new (winston.transports.File)({filename: './some_log_file.log'})
]
});
*/
let logger = new (winston.Logger)({
level: 'info',
transports: [
new (winston.transports.File)({filename: './some_log_file.log'})
]
})
let log = logger.log;
log('info', 'hello world');
Running the code above will result in the following error
/home/darren/projects/sp/node_modules/winston/lib/winston/logger.js:140
if (this._names.length === 0) {
^
TypeError: Cannot read property 'length' of undefined
at Logger.log (/home/darren/projects/sp/node_modules/winston/lib/winston/logger.js:140:18)
at Object.<anonymous> (/home/darren/projects/sp/testwinston.js:23:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Module.runMain (module.js:575:10)
at run (bootstrap_node.js:352:7)
at startup (bootstrap_node.js:144:9)
However, if we use the default logger, everything works just fine.
// outputs "info: hello world" to the console
const winston = require('winston');
let log = winston.log;
log('info', 'hello world');
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
log messages appearing twice with Python Logging
I'd say that they were incrementing on the log file, due to the fact that, when the application and the modules were loaded,...
Read more >logging.config — Logging configuration — Python 3.11.1 ...
The following functions configure the logging module. They are located in the logging.config module. Their use is optional — you can configure the...
Read more >Python Logging: In-Depth Tutorial - Toptal
Configure the root logger but never use it in your code—e.g., never call a function like logging.info() , which actually calls the root...
Read more >Python Logging Guide - Best Practices and Hands-on Examples
A stderr handler that formats log requests with INFO and higher logging level log via the simple formatter and emits the resulting log...
Read more >Log4j2 Example Tutorial - Configuration, Levels, Appenders
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. While processing the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We found a quick fix for this. Simply bind the logger to the log function:
var boundLogger = logger.log.bind(logger);
Then just use the boundLogger in place of logger.log.
The reason for doing it this way is to remain logger agnostic - not all loggers will have methods: info, error, and so-forth. Using just one method, log, keeps it simple.
Why not pass around the
logger
and uselogger.info('message')
logger.error('My error message')
? This is just how javascript works. You plucked a function without preserving its context. If you want to pass around thelog
function rather than thelogger
you should bind it yourself. I just think it makes more sense to pass around the logger as thats the point