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

Customizable log format #183

Closed
eugeneiiim opened this issue Nov 20, 2013 · 21 comments
Closed

Customizable log format #183

eugeneiiim opened this issue Nov 20, 2013 · 21 comments

Comments

@eugeneiiim
Copy link

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.

@yeruizhi
Copy link

+1. And model from where the log line was generated would be useful.

@rlidwka
Copy link
Collaborator

rlidwka commented Jan 15, 2014

json? bunyan? not sure it's a right way, just a wild thought

@godknowsiamgood
Copy link

+1. Can't wait for this feature.

@sailxjx
Copy link
Contributor

sailxjx commented Jan 28, 2014

Maybe you can try my small module graceful-logger.
This works well on my server.

@dandv
Copy link
Contributor

dandv commented Mar 7, 2014

👍 for timestamped logs.

2 similar comments
@rhhamburg
Copy link

👍 for timestamped logs.

@retargetly
Copy link

👍 for timestamped logs.

@nalindak
Copy link

👍 for timestamped logs.

@soyuka soyuka self-assigned this Jul 25, 2014
@Unitech
Copy link
Owner

Unitech commented Jul 28, 2014

^^'

Draft

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 ?

@soyuka
Copy link
Collaborator

soyuka commented Jul 28, 2014

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 (:.

@Unitech
Copy link
Owner

Unitech commented Jul 28, 2014

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) ?

@Unitech Unitech added the 0.9.3 label Jul 28, 2014
@sailxjx
Copy link
Contributor

sailxjx commented Jul 28, 2014

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) :).

@Unitech
Copy link
Owner

Unitech commented Jul 28, 2014

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

@sailxjx
Copy link
Contributor

sailxjx commented Jul 28, 2014

Thus moment would be a good choice ~

在 2014年7月28日,18:27,Alexandre Strzelewicz notifications@github.com 写道:

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.

@Unitech
Copy link
Owner

Unitech commented Jul 28, 2014

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 ?

@soyuka
Copy link
Collaborator

soyuka commented Jul 28, 2014

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.

@luchillo17
Copy link

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?

@soyuka
Copy link
Collaborator

soyuka commented Apr 26, 2016

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!

@luchillo17
Copy link

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?

@luchillo17
Copy link

luchillo17 commented Apr 26, 2016

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

@zorji
Copy link

zorji commented Sep 22, 2016

I have the same issue, --log-date-format doesn't work at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests