UNPKG

log4js

Version:
108 lines (81 loc) 8.64 kB
<!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="chrome=1"> <title>log4js-node by nomiddlename</title> <link rel="stylesheet" href="/assets/css/style.css?v=a7f232b4c6654881e6a8bd2ac48ee149603d74de"> <meta name="viewport" content="width=device-width"> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> </head> <body> <div class="wrapper"> <header> <h1>log4js-node</h1> <p>A port of log4js to node.js</p> <p class="view"><a href="http://github.com/nomiddlename/log4js-node">View the Project on GitHub <small></small></a></p> <ul> <li><a href="index.html">Home</a></li> <li><a href="api.html">API</a></li> <li><a href="appenders.html">Appenders</a></li> <li><a href="layouts.html">Layouts</a></li> <li><a href="terms.html">Terminology</a></li> <li><a href="faq.html">FAQ</a></li> <li><a href="contrib-guidelines.html">Want to help?</a></li> <li><a href="contributors.html">Contributors</a></li> </ul> </header> <section> <h1 id="date-rolling-file-appender">Date Rolling File Appender</h1> <p>This is a file appender that rolls log files based on a configurable time, rather than the file size. When using the date file appender, you should also call <code class="highlighter-rouge">log4js.shutdown</code> when your application terminates, to ensure that any remaining asynchronous writes have finished. Although the date file appender uses the <a href="https://github.com/nomiddlename/streamroller">streamroller</a> library, this is included as a dependency of log4js so you do not need to include it yourself.</p> <h2 id="configuration">Configuration</h2> <ul> <li><code class="highlighter-rouge">type</code> - <code class="highlighter-rouge">"dateFile"</code></li> <li><code class="highlighter-rouge">filename</code> - <code class="highlighter-rouge">string</code> - the path of the file where you want your logs written.</li> <li><code class="highlighter-rouge">pattern</code> - <code class="highlighter-rouge">string</code> (optional, defaults to <code class="highlighter-rouge">.yyyy-MM-dd</code>) - the pattern to use to determine when to roll the logs.</li> <li><code class="highlighter-rouge">layout</code> - (optional, defaults to basic layout) - see <a href="/layouts.html">layouts</a></li> </ul> <p>Any other configuration parameters will be passed to the underlying <a href="https://github.com/nomiddlename/streamroller">streamroller</a> implementation (see also node.js core file streams):</p> <ul> <li><code class="highlighter-rouge">encoding</code> - <code class="highlighter-rouge">string</code> (default “utf-8”)</li> <li><code class="highlighter-rouge">mode</code>- <code class="highlighter-rouge">integer</code> (default 0644)</li> <li><code class="highlighter-rouge">flags</code> - <code class="highlighter-rouge">string</code> (default ‘a’)</li> <li><code class="highlighter-rouge">compress</code> - <code class="highlighter-rouge">boolean</code> (default false) - compress the backup files during rolling (backup files will have <code class="highlighter-rouge">.gz</code> extension)</li> <li><code class="highlighter-rouge">alwaysIncludePattern</code> - <code class="highlighter-rouge">boolean</code> (default false) - include the pattern in the name of the current log file as well as the backups.</li> </ul> <p>The <code class="highlighter-rouge">pattern</code> is used to determine when the current log file should be renamed and a new log file created. For example, with a filename of ‘cheese.log’, and the default pattern of <code class="highlighter-rouge">.yyyy-MM-dd</code> - on startup this will result in a file called <code class="highlighter-rouge">cheese.log</code> being created and written to until the next write after midnight. When this happens, <code class="highlighter-rouge">cheese.log</code> will be renamed to <code class="highlighter-rouge">cheese.log.2017-04-30</code> and a new <code class="highlighter-rouge">cheese.log</code> file created. Note that, unlike the <a href="/file.html">file appender</a> there is no maximum number of backup files and you will have to clean up yourself (or submit a <a href="/contrib-guidelines.html">pull request</a> to add this feature). The appender uses the <a href="https://github.com/nomiddlename/date-format">date-format</a> library to parse the <code class="highlighter-rouge">pattern</code>, and any of the valid formats can be used. Also note that there is no timer controlling the log rolling - changes in the pattern are determined on every log write. If no writes occur, then no log rolling will happen. If your application logs infrequently this could result in no log file being written for a particular time period.</p> <h2 id="example-default-daily-log-rolling">Example (default daily log rolling)</h2> <div class="language-javascript highlighter-rouge"><pre class="highlight"><code><span class="nx">log4js</span><span class="p">.</span><span class="nx">configure</span><span class="p">({</span> <span class="na">appenders</span><span class="p">:</span> <span class="p">{</span> <span class="na">everything</span><span class="p">:</span> <span class="p">{</span> <span class="na">type</span><span class="p">:</span> <span class="s1">'dateFile'</span><span class="p">,</span> <span class="na">filename</span><span class="p">:</span> <span class="s1">'all-the-logs.log'</span> <span class="p">}</span> <span class="p">},</span> <span class="na">categories</span><span class="p">:</span> <span class="p">{</span> <span class="na">default</span><span class="p">:</span> <span class="p">{</span> <span class="na">appenders</span><span class="p">:</span> <span class="p">[</span> <span class="s1">'everything'</span> <span class="p">],</span> <span class="na">level</span><span class="p">:</span> <span class="s1">'debug'</span> <span class="p">}</span> <span class="p">}</span> <span class="p">});</span> </code></pre> </div> <p>This example will result in files being rolled every day. The initial file will be <code class="highlighter-rouge">all-the-logs.log</code>, with the daily backups being <code class="highlighter-rouge">all-the-logs.log.2017-04-30</code>, etc.</p> <h2 id="example-with-hourly-log-rolling-and-compressed-backups">Example with hourly log rolling (and compressed backups)</h2> <div class="language-javascript highlighter-rouge"><pre class="highlight"><code><span class="nx">log4js</span><span class="p">.</span><span class="nx">configure</span><span class="p">({</span> <span class="na">appenders</span><span class="p">:</span> <span class="p">{</span> <span class="na">everything</span><span class="p">:</span> <span class="p">{</span> <span class="na">type</span><span class="p">:</span> <span class="s1">'dateFile'</span><span class="p">,</span> <span class="na">filename</span><span class="p">:</span> <span class="s1">'all-the-logs.log'</span><span class="p">,</span> <span class="na">pattern</span><span class="p">:</span> <span class="s1">'.yyyy-MM-dd-hh'</span><span class="p">,</span> <span class="na">compress</span><span class="p">:</span> <span class="kc">true</span> <span class="p">}</span> <span class="p">},</span> <span class="na">categories</span><span class="p">:</span> <span class="p">{</span> <span class="na">default</span><span class="p">:</span> <span class="p">{</span> <span class="na">appenders</span><span class="p">:</span> <span class="p">[</span> <span class="s1">'everything'</span> <span class="p">],</span> <span class="na">level</span><span class="p">:</span> <span class="s1">'debug'</span><span class="p">}</span> <span class="p">}</span> <span class="p">});</span> </code></pre> </div> <p>This will result in one current log file (<code class="highlighter-rouge">all-the-logs.log</code>). Every hour this file will be compressed and renamed to <code class="highlighter-rouge">all-the-logs.log.2017-04-30-08.gz</code> (for example) and a new <code class="highlighter-rouge">all-the-logs.log</code> created.</p> </section> <footer> <p>This project is maintained by <a href="http://github.com/nomiddlename">nomiddlename</a></p> <p><small>Hosted on GitHub Pages &mdash; Theme by <a href="https://github.com/orderedlist">orderedlist</a></small></p> </footer> </div> <script src="/assets/js/scale.fix.js"></script> </body> </html>