It would be great to be able to specify the log format. For example, I would like to display a timestamp with each log line.
+1. And model from where the log line was generated would be useful.
json? bunyan? not sure it's a right way, just a wild thought
+1. Can't wait for this feature.
Maybe you can try my small module graceful-logger.
This works well on my server.
:+1: for timestamped logs.
:+1: for timestamped logs.
:+1: for timestamped logs.
:+1: for timestamped logs.
^^'
If using graceful-logger:
Formating logs via CLI:
$ pm2 start app.js --log-format ":date.color :level.blue :msg"
Formating logs via JSON:
{
"apps" : [{
"script" : "hello.js",
"name" : "hello",
"log_format" : ":date.color :level.blue :msg"
}]
}
Any thought @soyuka @dandv @rlidwka ?
It would be great having a moment format support with timestamp (X) the default.
I don't think you need to implement graceful-logger but having a log_date_format option could be nice. I was thinking about something like:
pm2 start --log-date-format "YYYYMMDD"
{
"apps" : [{
"script" : "hello.js",
"name" : "hello",
"log_date_format" : "YYY-MM-DD HH:mm Z"
}]
}
Everyone would be able to choose what he like most (:.
Thx @soyuka , momentjs is also a great module, if the need is only focused on having timestamped logs, this is the right solution. I will do that.
Just an idea, is it possible to print the file and the line where the log has been generated ? (by overriding, a clean way, the console.log and console.error) ?
I think moment is too heavy and may cause some efficiency problem, so I didn't require it in graceful-logger.
Pm2 just need a timestamped log, so I think put new Date on the top of each line will help (and simple) :).
momentjs is not that heavy, it doesnt require any dependencies and is only about 28k minified.
Concerning performance, yep there is a little overhead: http://jsperf.com/datejs-vs-moment-js/4
Thus moment would be a good choice ~
在 2014年7月28日,18:27,Alexandre Strzelewicz [email protected] 写道:
momentjs is not that heavy, it doesnt require any dependencies and is only about 28k minified.
Concerning performance, yep there is a little overhead: http://jsperf.com/datejs-vs-moment-js/4—
Reply to this email directly or view it on GitHub.
Wut ! I found something very interesting.
With that we can print the function name, line number, filename...
https://gist.github.com/Unitech/5a7e099d8c99b1b16d7c
Some other interesting methods: https://code.google.com/p/v8/wiki/JavaScriptStackTraceApi#Customizing_stack_traces
Extract:
getThis: returns the value of this
getTypeName: returns the type of this as a string. This is the name of the function stored in the constructor field of this, if available, otherwise the object's [[Class]] internal property.
getFunction: returns the current function
getFunctionName: returns the name of the current function, typically its name property. If a name property is not available an attempt will be made to try to infer a name from the function's context.
getMethodName: returns the name of the property of this or one of its prototypes that holds the current function
getFileName: if this function was defined in a script returns the name of the script
getLineNumber: if this function was defined in a script returns the current line number
getColumnNumber: if this function was defined in a script returns the current column number
getEvalOrigin: if this function was created using a call to eval returns a CallSite object representing the location where eval was called
isToplevel: is this a toplevel invocation, that is, is this the global object?
isEval: does this call take place in code defined by a call to eval?
isNative: is this call in native V8 code?
isConstructor: is this a constructor call?
Would you be interested by that ?
console functions are very easy to re-implement, basically it's just a:
var stream = process.stdout; //could be a fs.createWriteStream(path)
var log = function() {
if(process.argv.indexOf('-q') === -1 && process.argv.indexOf('-quiet') === -1) {
stream.write(util.format.apply(this, arguments) + EOL);
}
}
We could improve this by adding some time and an optional moment call:
var t = !date_log_format || date_log_format === 'X' ? Date.now() : moment().format(date_log_format);
stream.write(t + util.format.apply(this, arguments) + EOL);
@Unitech: if we want to implement stack traces here we should not do it by default, it could be a huge performance break.
Hello Unitech, question, i have tried to customize the format for timestamp in my logs however that doesn't work, i used the following:
pm2 start tao-app-rest --merge-logs --log-date-format="YYYY-MM-DD HH:mm Z"
However the logs after restart seems the same format, is it because it was an already started app? may i need to stop and kill the app to get the expected result?
Hmm, when you start an app the first time, pm2 will hold those parameters in-memory. They should be replaced when you issue a restart with new specified parameters.
Maybe that on restart the log format was overridden?
may i need to stop and kill the app to get the expected result?
May you test this? It would be kind to have reproducible steps if this still doesn't work for you, thanks!
I didn't issue a restart with params, the app's where up and i just used the start command like up there, so if i did use start with format but then input a restart those options dissapear?
If i input the start command 2 times both with log format params, the second one should output logs with the date and time but it only shows that for PM2 logs, app logs don't show neither of them:
$ pm2 flush && pm2 start tao-app-rest --merge-logs --log-date-format="YYYY-MM-DD HH:mm Z" && pm2 start tao-app-rest --merge-logs --log-date-format="YYYY-MM-DD HH:mm Z"
[PM2] Flushing
[PM2] /root/.pm2/logs/tao-app-rest-out-1.log
[PM2] /root/.pm2/logs/tao-app-rest-error-1.log
[PM2] restartProcessId process id 1
[PM2] Process successfully started
...
[PM2] restartProcessId process id 1
[PM2] Process successfully started
...
$ pm2 logs
[PM2] Tailing last 20 lines for [all] processes
PM2: 2016-04-26 11:24:03: Stopping app:tao-app-rest id:1
PM2: 2016-04-26 11:24:03: App [tao-app-rest] with id [1] and pid [5645], exited with code [0] via signal [null]
PM2: 2016-04-26 11:24:03: Starting execution sequence in -fork mode- for app name:tao-app-rest id:1
PM2: 2016-04-26 11:24:03: App name:tao-app-rest id:1 online
PM2: 2016-04-26 11:24:15: Stopping app:tao-app-rest id:1
PM2: 2016-04-26 11:24:15: App [tao-app-rest] with id [1] and pid [5694], exited with code [0] via signal [null]
PM2: 2016-04-26 11:24:15: Starting execution sequence in -fork mode- for app name:tao-app-rest id:1
PM2: 2016-04-26 11:24:15: App name:tao-app-rest id:1 online
PM2: 2016-04-26 11:24:24: Stopping app:tao-app-rest id:1
PM2: 2016-04-26 11:24:24: App [tao-app-rest] with id [1] and pid [5723], exited with code [0] via signal [null]
PM2: 2016-04-26 11:24:24: Starting execution sequence in -fork mode- for app name:tao-app-rest id:1
PM2: 2016-04-26 11:24:24: App name:tao-app-rest id:1 online
tao-app-rest-1 (out): Servidor corriendo en el puerto 50003 // }
tao-app-rest-1 (out): Servidor corriendo en el puerto 50003 // } } Here's no date/time logging
tao-app-rest-1 (out): Servidor corriendo en el puerto 50003 // }
[PM2] Streaming realtime logs for [all] processes
I have the same issue, --log-date-format doesn't work at all
Most helpful comment
I have the same issue, --log-date-format doesn't work at all