How to log - the 12 factor application way

2019-06-16 03:39发布

I want to know the best practice behind logging my node application. I was reading the 12 factor app guidelines at https://12factor.net/logs and it states that logs should always be sent to the stdout. Cool, but then how would someone manage logs in production? Is there an application that scoops up whatever is sent to stdout? In addition, is it recommended that I only be logging to stdout and not stderr? I would appreciate a perspective on this matter.

2条回答
对你真心纯属浪费
2楼-- · 2019-06-16 04:01

Is there an application that scoops up whatever is sent to stdout?

The page you linked to provides some examples of log management tools, but the simplest version of this would be just redirecting the output of your application to a file. So in bash node app.js > app.out. You could also split your stdout and stderr like node app.js 2> app.err 1> app.out.

You could additionally have some sort of service that collects the logs from this file, and then puts them indexes them for searching somewhere else.

The idea behind the suggestion to only log to stdout is to let the environment control what to do with the logs because the application doesn't necessarily know the environment that it will eventually run within. Furthermore, by treating all logs as an event stream, you leave the choice of what to do with this stream up to the environment. You may want to send the log stream directly to a log aggregation service for instance, or you may want to first preprocess it, and then stream the result somewhere else. If you mandate a specific output such as logging to a file, you reduce the portability of your service.

Two of the primary goals of the 12 factor guidelines are to be "suitable for deployment on modern cloud platforms" and to offer "maximum portability between execution environments". On a cloud platform where you might have ephemeral storage on your instance, or many instances running the same service, you'd want to aggregate your logs into some central store. By providing a log stream, you leave it up to the environment to coordinate how to do this. If you put them directly into a file, then you would have to tailor your environment to wherever each application has decided to put the logs in order to then redirect them to the central store. Using stdout for logs is thus primarily a useful convention.

查看更多
走好不送
3楼-- · 2019-06-16 04:07

I think it's a mistake to categorically say "[web] applications should write logs to stdout".

Rather, I would suggest:

a) Professional-quality, robust web apps should HAVE logs

b) The application should treat the "log" as an abstract, "stream" object

c) Ideally, the logger implementation MAY be configured to write to stdout, to stderr, to a file, to a date-stamped file, to a rotating file, filter by severity level, etc. etc. as appropriate.

I would strongly argue that hard-coded writes to stdout, without any intervening "logger" abstraction, is POOR practice.

Here is a good article:

https://blog.risingstack.com/node-js-logging-tutorial/

查看更多
登录 后发表回答