-
Notifications
You must be signed in to change notification settings - Fork 36
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
Comments
I created this plugin as a proof of concept to extend logdown https://github.com/caiogondim/logdown-print-method-name.js What do you think? |
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 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',
}) |
I kinda agree that timestamp is core, but I like the idea of using a plugin more. 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. |
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 ( 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. |
Not actually. The initial idea of the lib was to wrap
You don't have to import const logdown = withTimestamp(logdown('foo'))
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. |
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
Proposed Output
The text was updated successfully, but these errors were encountered: