NodeJS日誌管理 Log4js

來源:互聯網
上載者:User

標籤:

簡述:

記錄配置nodejs-log4js日誌輸出

參考: http://cnodejs.org/topic/521ee22abee8d3cb12fc1f02

            https://github.com/nomiddlename/log4js-node/wiki/Date%20rolling%20file%20appender


步驟:

1. 進入Express項目根路徑, 下載log4js的庫檔案

[plain] view plaincopy
  1. npr install log4js  


2. 配置log4js檔案

Logger.js

[javascript] view plaincopy
  1. /** 
  2.  * log4js 日誌輸出設定檔 
  3.  * @type {exports} 
  4.  */  
  5. var log4js = require(‘log4js‘);  
  6.   
  7. // logger configure  
  8. log4js.configure({  
  9.     appenders: [  
  10.         { type: ‘console‘ }, {  
  11.             type: ‘dateFile‘,  
  12.             filename: ‘logs/log‘,  
  13.             pattern: "_yyyy-MM-dd",  
  14.             maxLogSize: 1024,  
  15.             alwaysIncludePattern: false,  
  16.             backups: 4,  
  17.             category: ‘logger‘  
  18.         }  
  19.     ],  
  20.     replaceConsole: true  
  21. });  
  22.   
  23. module.exports = log4js.getLogger(‘logger‘);  



將Logger模組化聲明

啟動項目後,在logs檔案夾下立即會產生log檔案,

文檔中之初,dateFile檔案會在每天的midnight更新一下檔案,前一日的檔案會自動添上date format的結尾

eg,

修改windows時間



當天midnight之前



之後logs檔案夾會產生



NodeJS日誌管理 Log4js

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.