要配置Ubuntu上的JavaScript應用程序的日志級別,您需要遵循以下步驟:
確定您的JavaScript應用程序使用的日志庫。不同的庫可能有不同的配置方法。一些常見的日志庫包括Winston、Bunyan和Morgan。
查閱所選日志庫的文檔以了解如何設置日志級別。以下是一些常見庫的配置示例:
Winston:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info', // 設置日志級別為'info'
format: winston.format.json(),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'combined.log' }),
],
});
Bunyan:
const bunyan = require('bunyan');
const logger = bunyan.createLogger({
name: 'myApp',
streams: [
{
level: 'info', // 設置日志級別為'info'
stream: process.stdout,
},
{
level: 'error', // 設置錯誤日志級別為'error'
path: 'error.log',
},
],
});
Morgan:
const express = require('express');
const morgan = require('morgan');
const app = express();
app.use(morgan('combined')); // 使用Morgan的默認日志格式
根據您的需求調整日志級別。常見的日志級別包括:‘error’、‘warn’、‘info’、‘http’(僅在Express中使用)和’debug’。將日志級別設置為’error’將僅記錄錯誤,而設置為’info’將記錄更多詳細信息。
如果您的應用程序使用了多個日志庫,請確保為每個庫分別設置日志級別。
保存更改并重新啟動應用程序以使配置生效。
請注意,這些示例僅適用于Node.js環境中的JavaScript應用程序。如果您使用的是其他類型的JavaScript環境(如瀏覽器或前端框架),則需要查閱相應環境的文檔以了解如何設置日志級別。