logdown.js icon indicating copy to clipboard operation
logdown.js copied to clipboard

Add timestamps to log messages

Open bennycode opened this issue 7 years ago • 5 comments

In Chrome you can use "Show timestamps" to see timestamps next to your log messages. Unfortunately, this does not work in Node.js. Therefore we should add an option to automatically print timestamps together with the log message.

Proposal

const logdown = require('logdown')
const logger = logdown('MyLogger', { markdown: false, timestamp: true })
logger.log('Hello, World!')

Proposed Output

[2017-03-13 12:15:03.137] MyLogger Hello, World!

bennycode avatar Mar 13 '18 13:03 bennycode

I created this plugin as a proof of concept to extend logdown https://github.com/caiogondim/logdown-print-method-name.js Guess it's better if we leave core as lean as possible and create a great plugin ecosystem around it.

What do you think?

caiogondim avatar Mar 30 '18 15:03 caiogondim

It's great that logdown is extensible! I really like such architecture!

However, I think logging the time is a core functionality of a log library. There is a reason that Google implemented timestamps for console.log statements in Chrome.

As a compromise it would be great if we can supply a hook for the formatter, which makes it easier to modify texts before they get logged. Here is a great example from js-logger:

Logger.createDefaultHandler({
	formatter: function(messages, context) {
		// prefix each log message with a timestamp.
		messages.unshift(new Date().toUTCString())
	}
});

Currently I am extending logdown with timestamps like this:

import * as moment from 'moment'

class LogUtil {
  static padding: number = 25

  static addTimestamp(transObj: any) {
    if (~transObj.msg.indexOf('MyNamespace::')) {
      transObj.args.unshift(`[${moment().format('HH:mm:ss')}]`)
    }
  }

  static createName(className: string): string {
    className = `moon:${className}`
    const name = className.split('')
    while (name.length < this.padding) name.push(' ')
    return name.join('')
  }
}

export default LogUtil
const logdown = require('logdown')
logdown.transports = [LogUtil.addTimestamp]

const logger = logdown(LogUtil.createName('MyLogger'), {
  logger: console,
  prefixColor: '#3a38e8',
})

bennycode avatar Mar 30 '18 16:03 bennycode

I kinda agree that timestamp is core, but I like the idea of using a plugin more. Using a plugin is straightforward and we don't add more code to the core.

const pipe = require('tubo')
const logdown = require('logdown')
const withTimestamp = require('logdown-with-timestamp')

const logdown = pipe(
  logdown('foo'),
  withTimestamp
)

That makes logdown slim, fast and easier to test. We should delegate more to the community and make it easy to extend the library instead of trying to solve all problems ourselves.

caiogondim avatar Apr 02 '18 17:04 caiogondim

Wouldn't you agree that having timestamps is more essential than having markdown support for log messages? If that's the case, then we should also outsource the markdown rendering into a plugin.

For me "logdown" is a very nice wrapper around "console.log", which extends the functionality of log statements quite much. If we now start putting every little function (like timestamps) in a plugin, then "logdown" misses its purpose for me because I want an easy-to-use logging library and not a plugin which needs another plugin to work.

Log statements are small one-liners that programmers write in their source code. Imagine the frustation of people, when they now have to import 3 packages (tubo, logdown & logdown-with-timestamp) in order to log a single line of text. That's far from being handy.

It's okay when developers at Babel or Webpack build an ecosystem around their software, because they have a whole framework to offer. But establishing a community and an ecosystem for a logging utility sounds over ambitious to me.

bennycode avatar Apr 02 '18 17:04 bennycode

Wouldn't you agree that having timestamps is more essential than having markdown support for log messages? If that's the case, then we should also outsource the markdown rendering into a plugin.

Not actually. The initial idea of the lib was to wrap console with a markdown parser and debug compatibility. But again, I'm open to adding a timestamp capability.

Log statements are small one-liners that programmers write in their source code. Imagine the frustation of people, when they now have to import 3 packages (tubo, logdown & logdown-with-timestamp) in order to log a single line of text. That's far from being handy.

You don't have to import tubo. That's is just a wrapper for better pipeline pattern.

const logdown = withTimestamp(logdown('foo'))

It's okay when developers at Babel or Webpack build an ecosystem around their software, because they have a whole framework to offer. But establishing a community and an ecosystem for a logging utility sounds over ambitious to me.

To create a "ecosystem" is not the goal I first mentioned, but rather keeping the library as simple as possible. And extensible.

But again, timestamp is something that makes sense to have in the library. Go ahead if you fancy implementing it.

caiogondim avatar Apr 03 '18 15:04 caiogondim