question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The function 'log' is not bound with logger instance, resulting error when calling function 'log' without the logger instance

See original GitHub issue

My 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:closed
  • Created 6 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
Celadoracommented, Sep 26, 2017

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.

1reaction
jcrugzzcommented, Aug 24, 2017

Why not pass around the logger and use logger.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 the log function rather than the logger you should bind it yourself. I just think it makes more sense to pass around the logger as thats the point

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found