日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用

發布時間:2023/12/19 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

winston 被設計為一個簡單且通用的日志庫,支持多種傳輸。 傳輸本質上是日志的存儲設備。 每個 winston 記錄器都可以在不同級別配置多個存儲渠道。例如,人們可能希望將錯誤日志存儲在持久的遠程位置(如數據庫),但所有調試日志都輸出到控制臺或本地文件。

使用 winston 的推薦方法是創建您自己的記錄器。 最簡單的方法是使用 winston.createLogger:

const winston = require('winston');const logger = winston.createLogger({level: 'info',format: winston.format.json(),defaultMeta: { service: 'user-service' },transports: [//// - Write all logs with level `error` and below to `error.log`// - Write all logs with level `info` and below to `combined.log`//new winston.transports.File({ filename: 'error.log', level: 'error' }),new winston.transports.File({ filename: 'combined.log' }),], });// // If we're not in production then log to the `console` with the format: // `${info.level}: ${info.message} JSON.stringify({ ...rest }) ` // if (process.env.NODE_ENV !== 'production') {logger.add(new winston.transports.Console({format: winston.format.simple(),})); }

winston 的日志等級

const levels = { error: 0,warn: 1,info: 2,http: 3,verbose: 4,debug: 5,silly: 6 };

如何創建 logger

const logger = winston.createLogger({transports: [new winston.transports.Console(),new winston.transports.File({ filename: 'combined.log' })] });

即使 logger 實例創建之后,也能容易地刪除或者增添新的 transport:

const files = new winston.transports.File({ filename: 'combined.log' }); const console = new winston.transports.Console();logger.clear() // Remove all transports.add(console) // Add console transport.add(files) // Add file transport.remove(console); // Remove console transport

也能使用 logger 的 configure 方法,重新配置新的 transport:

const logger = winston.createLogger({level: 'info',transports: [new winston.transports.Console(),new winston.transports.File({ filename: 'combined.log' })] });// // Replaces the previous transports with those in the // new configuration wholesale. // const DailyRotateFile = require('winston-daily-rotate-file'); logger.configure({level: 'verbose',transports: [new DailyRotateFile(opts)] });

在 winston 中,Logger 和 Transport 實例都被視為接受 info 對象的 objectMode 流。

提供給給定格式的 info 參數表示單個日志消息。 對象本身是可變的。 每個信息必須至少具有 level 和 message 屬性:

const info = {level: 'info', // Level of the logging message message: 'Hey! Log something?' // Descriptive message being logged. };

除了級別和消息之外的屬性被視為“元屬性”:

const { level, message, ...meta } = info;

我們來動手寫一個實際的例子。

主程序:

// @ts-nocheck var express = require('express'); var app = express(); var DEFAULTPORT = 3003; var port = process.env.PORT || DEFAULTPORT; var winstonTest = require("./winstonTest");app.get('/', function(_req, res){res.send("Hello world");winstonTest.logMessage('234'); });app.listen(port, function(){console.log("App listens on port: " + port); });

winstonTest 的實現:

const winston = require('winston');const logger = winston.createLogger({level: 'info',format: winston.format.json(),defaultMeta: { service: 'user-service' },transports: [//// - Write all logs with level `error` and below to `error.log`// - Write all logs with level `info` and below to `combined.log`//new winston.transports.File({ filename: 'data/error.log', level: 'error' }),new winston.transports.File({ filename: 'data/combined.log' }),] });if (process.env.NODE_ENV !== 'production') {logger.add(new winston.transports.Console({format: winston.format.simple(),})); }console.log('Logger initialized successfully!');function log(content){logger.info('hello', { message: content }); }module.exports = {logMessage: log };

瀏覽器里輸入如下 url:
http://localhost:3003/
在 combined.log 里生成了如下 log 文件:

這就是 winston 最基本的使用方法。

總結

以上是生活随笔為你收集整理的日志库 winston 的学习笔记 - 创建一个使用 winston 的 Node.js 应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。