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

Add timestamps to log messages #142

Open
bennycode opened this issue Mar 13, 2018 · 5 comments
Open

Add timestamps to log messages #142

bennycode opened this issue Mar 13, 2018 · 5 comments

Comments

@bennycode
Copy link
Collaborator

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!

@caiogondim
Copy link
Owner

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?

@bennycode
Copy link
Collaborator Author

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',
})

@caiogondim
Copy link
Owner

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.

@bennycode
Copy link
Collaborator Author

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.

@caiogondim
Copy link
Owner

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.

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