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

常用的網站後台Logger Library #22

Open
sj82516 opened this issue Mar 31, 2017 · 2 comments
Open

常用的網站後台Logger Library #22

sj82516 opened this issue Mar 31, 2017 · 2 comments

Comments

@sj82516
Copy link

sj82516 commented Mar 31, 2017

目的

我希望可以再網站後台使用Logger System,記錄每位使用者的Request Method、以及錯誤訊息,有以下需求

  1. 可以分類:如info/ warn/ error
  2. 上述分類可以導出到不同File,如server-info.log / server-warn.log
  3. 完整輸出錯誤

使用的工具

我在 Mac 10.10 下使用 npm 安裝 node.js 7.6.0 ,使用sails.js 0.12

遇到的問題

我原本使用winston,為了要可以分類輸出log,我參考以下做法

const winston = require('winston');

// let debug = new winston.Logger({
//     levels: {
//         debug: 0
//     },
//     transports: [
//         new (winston.transports.File)({ filename: './logs/debug.log', color: false,level: 'debug'}),
//         new (winston.transports.Console)({level: 'debug'})
//     ]
// });

let info = new winston.Logger({
    levels: {
        info: 1
    },
    transports: [
        new (winston.transports.File)({ filename: './logs/info.log', level: 'info'}),
        new (winston.transports.Console)({level: 'info'})
    ]
});

let warn = new winston.Logger({
    levels: {
        warn: 2
    },
    transports: [
        new (winston.transports.File)({ filename: './logs/warning.log', level: 'warn'}),
        new (winston.transports.Console)({level: 'warn'})
    ]
});

let error = new winston.Logger({
    levels: {
        error: 3
    },
    transports: [
        new (winston.transports.File)({ filename: './logs/error.log', level: 'error'}),
        new (winston.transports.Console)({level: 'error'})
    ]
});

module.exports = {
    info: function(msg){
        info.info(msg);
    },
    warn: function(msg){
        warn.warn(msg);
    },
    error: function(msg){
        error.error(msg);
    },
    log: function(level,msg){
        let lvl = exports[level];
        lvl(msg);
    }
};

但是發現 他會出現資料掉失的問題,例如說我插入middlewear想要打印全部的HTTP Request

myRequestLogger: function (req, res, next) {
            console.log(req.method, req.url , req.method, req.body, req.query);
            logger.info("Requested :: " + ", req-method:" + req.method +
                ", req-url:" + req.url +
                req.method==='POST' ? ", req-body"+req.body:"," +
                req.method==='GET' ? ", req-query" + req.query: "");
            return next();
        },

發現winston並沒有輸出應有的資訊,我有使用console.log作為比對,console.log顯示為正確資訊

嘗試過的解法

我有照Sails官網的做法sails.config.log會有同樣的資訊不完整或是遺失的問題

@sj82516
Copy link
Author

sj82516 commented Mar 31, 2017

抱歉我剛才發現錯誤了,忘記+的優先順序高於?判斷式,所以會導致輸出錯誤。
麻煩管理員刪除Issue,謝謝

@billiebbb
Copy link

sails 本身就有帶 log module 了,是可以指定輸出的 file path 的

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

No branches or pull requests

2 participants