PHP前端开发

记录 Nodejs 应用程序的最佳实践

百变鹏仔 3天前 #JavaScript
文章标签 应用程序

作为一名 node.js 开发人员,在调试、监控和维护应用程序时,日志记录几乎就是一切。但您是否使用日志记录最佳实践?让我们探索一些可以将您的 node.js 应用程序提升到新水平的日志记录技术。

要了解更多信息,您可以查看完整的博客文章。

1. 温斯顿:伐木界的瑞士军刀

?工具:温斯顿
? 描述:node.js 的多功能日志库
? 主要特点:

javascriptcopyconst winston = require('winston');const logger = winston.createlogger({  level: 'info',  format: winston.format.json(),  transports: [    new winston.transports.file({ filename: 'error.log', level: 'error' }),    new winston.transports.file({ filename: 'combined.log' })  ]});

2. morgan:http请求记录器中间件

?工具:摩根
? 描述:简化 express.js 中的 http 请求日志记录
? 主要特点:

javascriptcopyconst express = require('express');const morgan = require('morgan');const app = express();app.use(morgan('combined'));

3. bunyan:node.js 的 json 日志记录

?工具:bunyan
? 描述:node.js 应用程序的结构化 json 日志记录
? 主要特点:

javascriptcopyconst bunyan = require('bunyan');const log = bunyan.createlogger({name: "myapp"});log.info("hi");log.warn({lang: 'fr'}, "au revoir");

4. pino:超快速 node.js 记录器

?工具:皮诺
? 描述:使用 json 输出进行低开销日志记录
? 主要特点:

javascriptcopyconst pino = require('pino');const logger = pino();logger.info('hello world');logger.error('this is at error level');

5. 调试:微型调试实用程序

? 工具:调试
? 描述:node.js 的小型调试实用程序
? 主要特点:

javascriptcopyconst debug = require('debug')('http');debug('booting %o', name);

6. log4js:灵活的 javascript 日志记录

?工具:log4js
? 描述:log4j 框架到 javascript 的转换
? 主要特点:

javascriptcopyconst log4js = require("log4js");log4js.configure({  appenders: { cheese: { type: "file", filename: "cheese.log" } },  categories: { default: { appenders: ["cheese"], level: "error" } }});const logger = log4js.getlogger("cheese");logger.error("cheese is too ripe!");

7.elasticsearch、logstash 和 kibana(elk 堆栈)

?工具:elk stack
? 描述:日志管理和分析的强大组合
? 主要特点:

javascriptcopyconst winston = require('winston');const elasticsearch = require('winston-elasticsearch');const estransportopts = {  level: 'info',  clientopts: { node: 'http://localhost:9200' }};const logger = winston.createlogger({  transports: [    new elasticsearch(estransportopts)  ]});

8.sentry:错误跟踪和性能监控

? 工具:哨兵
? 描述:实时错误跟踪和性能监控
? 主要特点:

javascriptcopyconst sentry = require("@sentry/node");sentry.init({ dsn: "https://examplepublickey@o0.ingest.sentry.io/0" });try {  somefunction();} catch (e) {  sentry.captureexception(e);}

9. 新遗迹:应用程序性能监控

? 工具:新遗物
? 描述:全面的应用程序性能监控
? 主要特点:

javascriptcopyconst newrelic = require('newrelic');newrelic.settransactionname('mycustomtransaction');// your application code here

10. loggly:基于云的日志管理

?工具:loggly
? 描述:基于云的日志管理和分析服务
? 主要特点:

javascriptcopyconst winston = require('winston');const { loggly } = require('winston-loggly-bulk');winston.add(new loggly({    token: "your-token",    subdomain: "your-subdomain",    tags: ["winston-nodejs"],    json: true}));

winston.log('info', "来自 node.js 的 hello world!");

额外提示:结构化日志记录

无论您选择什么工具,实现结构化日志记录都可以极大地提高您的日志分析能力:

javascriptCopylogger.info({  event: 'user_login',  userId: user.id,  timestamp: new Date().toISOString(),  ipAddress: req.ip});

通过使用这些附加工具和实践,您将拥有全面的日志记录策略,涵盖从基本调试到高级应用程序性能监控的所有内容。请记住,有效日志记录的关键是根据您的特定需求选择正确的工具,并在整个代码库中持续应用最佳实践。

如果您在调试 web 应用程序时需要帮助,请查看 https://alerty.ai 以了解有关简单前端监控的更多信息。

祝您日志记录愉快,祝您的 node.js 应用程序顺利运行! ??