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.

Add timestamps to log messages

See original GitHub issue

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!

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bennycodecommented, Mar 30, 2018

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',
})
0reactions
caiogondimcommented, Apr 3, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add timestamps to log messages - xMatters Support
After this, instead of using "console.log('Your message here');" each time you want to include a timestamp, simply use: tsLog( 'Great news!' );.
Read more >
include timestamp in log messages #38410 - JuliaLang/julia
To include the timestamp, one currently has to add the now function to each log call, e.g.. julia> @info "$(now(UTC)) hello world" [...
Read more >
Print timestamp for logging in Python - Stack Overflow
Before the first time you log anything do this: logging.basicConfig( format='%(asctime)s %(levelname)-8s %(message)s', level=logging.
Read more >
How to add a timestamp to bash script log? - linux - Server Fault
When you need to send to log file use #!/usr/bin/env bash source /path/to/config.sh echo "$TIMESTAMP Say what you are doing" >> $LOGFILE do_what_you_want ......
Read more >
How can I add timestamp to console.log? - Forums - IBM
Is there a way to add timestamps into console.log? ... If you look at messages.log or trace.log in the usr/servers/myServer/logs directory, they both...
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