npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

logger-email

v1.0.5

Published

express+node+log4js+ejs日志系统,包含邮件功能

Downloads

2

Readme

logger-email


基于node+log4js+express+nodemailer+node-schedule 的日志系统

install

npm i logger-email --save or cnpm i logger-eamil --save

modules

use

1.在入口文件,app.js中使用

app.js:

1.需要先引入logger-email/config,配置log和email的参数; 2.配置完日志和邮箱的参数后,才可以require('logger-email'),否则配置不生效;

var loggerConfig = require('logger-email/config');
// 设置日志配置项
loggerConfig.setLogConfig({
    compress: true, //是否在生成第二个文件时将上一个压缩 如:error_2018-10-30.log.gz
    logPath: path.join(__dirname,'./defaultlogs/'),
    daysToKeep: 7,//保存7天,log4不起作用,单独方法删除文件
    attachments: true,// true 为发送附件,默认false
    onLineLink: 'http://127.0.1:3000/defaultlogs/' // send:link<required> 可访问的域名地址,查看日志用
});

// 设置邮件配置项(如果需要发送邮件)
loggerConfig.setEmailConfig({
    host: 'smtp.exmail.qq.com',
    port: 465,
    secure: true,// true for 465, false for other ports
    user: 'user email', // generated ethereal user
    pass: 'password', // generated ethereal password 
    from: 'sender email address', // sender address
    to: '**@163.com,**@qq.com', // list of receivers 多个邮箱","分开
    canSend: true,//是否可以发送邮件(false:不发送;true:发送)
    subject: 'test email subject'
});

// 要保证链接能访问到日志,这里的 `/defaultlogs` 路由要和 `loggerConfig.setLogConfig`中配置的`onLineLink`保持一直,否则邮件中的日志地址就不正确了
app.use('/defaultlogs',express.static(path.join(__dirname, '../defaultlogs')));

// 这句一定要在配置完`setLogConfig`和`setEmailConfig`后出现,否则配置会不生效,其他js需要引入时可以直接引入
var logger = require('logger-email');

2. 捕获全局异常请求

3.在app.js中的全局catch error 方法里捕获异常;

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
app.use(function (err, req, res, next) {
    // output error logs 
    logger.template.appJsError(err,req);
    // set locals, only providing error in development
    res.statusCode = 404;
    res.render('error',{
        content: err
    });
});

3. 捕获ejs报错,导致页面空白


var logger = require('logger-email');
var manualLog = logger.getLogger('manual'); // 手动输出

app.use('/home',function (req,res,next){
    res.render('index',{
        title: 'aha'
    },function (err,html){
        if (err){
            logger.template.ejsRenderError({
                page:'/home',
                req: req,
                error: err
            });
        }
        res.send(html);
    });
})

使用模板

logger.template.appJsError(err,req); app.js中捕获异常用 logger.template.emailLogger(object); 调用时会保存日志同时发送邮件到指定的邮箱 logger.template.requestCatchError(object); 请求接口时try catch 到error时使用 logger.template.requestError(object); 请求接口时状态异常使用 logger.template.ejsRenderError(object); ejs 渲染参数出错,导致空白页面时用

格式:
  • appJsError
/**
 * @param {Object} err 捕获到的错误信息
 * @param {Object} req request
 */
logger.template.appJsError(err,req);
  • requestError || requestCatchError || requestError
/**
 * @param {Number} durTime 接口耗时
 * @param {String} method:请求方法
 * @param {Object} req:request
 * @param {String} url: 接口地址或者页面地址
 * @param {Object} urlPara:请求参数
 * @param {Any} error:error信息
 * @param {Any} body:返回数据
 */
logger.template.requestError({
                    durTime: durTime, 
                    method: 'post',
                    req: request,
                    url: url,
                    urlPara: urlPara,
                    error: err,
                    body: body
                });
  • ejsRenderError 调用这个方法时会发送日志到指定邮箱
/**
 * @param {String} page 当前渲染ejs模板的模板路径
 * @param {Object} req:request
 * @param {Any} error:error信息
 */
logger.template.requestError({
                    page: '当前ejs模板地址',
					req: request,
					error: error信息
                });

输出日志对应关系

level : [trace,debug,info,warn,error,fatal]

默认logger 为 :var logger = require('logger-email');

  • var errorLog = logger.getLogger('error'); // errorLog.error('logs'); || errorLog.fatal('logs'); 能保存到日志文件

  • var requestLog = logger.getLogger('request'); // requestLog.error('logs'); || requestLog.fatal('logs'); 能保存到日志文件

  • var manualLog = logger.getLogger('manual'); // manualLog.warn('logs'); || manualLog.error('logs');|| manualLog.fatal('logs'); 能保存到日志文件

  • var defaultLog = logger.getLogger('default'); // 只在控制台输出,不保存到日志文件

  • 以上errorLog,requestLog,manualLog会输出日志到对应的文件: errorLog-------------> /defaultlogs/error/error_2018-12-06.log requestLog-----------> /defaultlogs/request/request_2018-12-06.log manualLog------------> /defaultlogs/manual/manual_2018-12-06.log

app.js 完整代码:

var express = require('express');
var path = require('path');
var fs = require('fs');
var router = express.Router();
var app = express();

var loggerConfig = require('logger-email/config');
// 设置日志配置项
loggerConfig.setLogConfig({
    compress: true, //是否在生成第二个文件时将上一个压缩 如:error_2018-10-30.log.gz
    logPath: path.join(__dirname,'./defaultlogs/'),
    daysToKeep: 7,//保存7天,log4不起作用,单独方法删除文件
    attachments: true,// true 为发送附件,默认false
    onLineLink: 'http://127.0.1:3000/defaultlogs/' // send:link<required> 可访问的域名地址,查看日志用
});

// 设置邮件配置项(如果需要发送邮件)
loggerConfig.setEmailConfig({
    host: 'smtp.exmail.qq.com',
    port: 465,
    secure: true,// true for 465, false for other ports
    user: '****@qq.com', // generated ethereal user
    pass: '****', // generated ethereal password 
    from: '****@qq.com', // sender address
    to: '***@163.com', // list of receivers 多个邮箱","分开
    canSend: true,//是否可以发送邮件(false:不发送;true:发送)
    subject: '测试邮件啊啊',
});

// 要保证链接能访问到日志
app.use('/defaultlogs',express.static(path.join(__dirname, '../defaultlogs')));

var logger = require('logger-email');
var manualLog = logger.getLogger('manual'); // 手动输出
logger.sendDayLog();// 每日定时发送邮件
logger.appLogger(app); // 控制台输出日志

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use('/home',function (req,res,next){
    res.render('index',{
        title: 'aha'
    },function (err,html){
        manualLog.error("出错了");
        if (err){
            logger.template.ejsRenderError({
                page:'/home',
                req: req,
                error: err
            });
        }
        res.send(html);
    });
})

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handler
app.use(function (err, req, res, next) {
    // output error logs 
    logger.template.appJsError(err,req);
    // set locals, only providing error in development
    res.statusCode = 404;
    res.render('error',{
        content: err
    });
});

app.listen(3000,function (){
    console.log(`server is running at: 
    \n =============================== 
    \n ^_^ logs ^_^
    \n http://127.0.0.1:3000 
    \n ===============================`);
});

License


logger-email is licensed under the MIT license