diff --git a/app.js b/app.js index 475943e..6f61021 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,6 @@ const express = require("express"); const request = require("request"); -const fs = require("fs"); +const log = require("./log") const app = express(); const port = 9000; const { getDecryptionArray } = require("./decode"); @@ -27,6 +27,8 @@ function xorTransform(decryptionArray) { app.get("/download", (req, res) => { const { url, decodeKey } = req.query; + log.info(`download url:${url}`); + log.info(`download decodeKey:${decodeKey}`); let xorStream = xorTransform( getDecryptionArray(decodeURIComponent(decodeKey)) ); @@ -34,5 +36,5 @@ app.get("/download", (req, res) => { }); app.listen(port, () => { - console.log(`Example app listening at http://localhost:${port}`); + log.info(`Example app listening at http://localhost:${port}`); }); diff --git a/log.js b/log.js new file mode 100644 index 0000000..9b78c3b --- /dev/null +++ b/log.js @@ -0,0 +1,53 @@ +const winston = require('winston'); +function thisLine() { + try { + const e = new Error(); + const regex = /\((.*):(\d+):(\d+)\)$/ + const stacks = e.stack.split("\n") + const temp = [] + stacks.forEach(stack => { + if (!stack.includes("node_modules")) { + temp.push(stack) + } + }) + const match = regex.exec(temp[3]); + return { + filepath: match[1], + line: match[2], + column: match[3] + }; + } catch (e) { + return { + filepath: "unknow", + line: "0", + column: "0" + }; + } +} +const logger = winston.createLogger({ + level: 'info', + transports: [ + new winston.transports.Console(), + new winston.transports.File({ + dirname: 'log', filename: 'server.log' + }), + ], + datePattern: 'YYYY-MM-DD', + zippedArchive: true, + format: winston.format.combine( + winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss,SSS" }), + winston.format.align(), + winston.format.splat(), + winston.format.printf( + (info) => { + const { timestamp, level, message } = info; + const { filepath, line } = thisLine() + return `[${timestamp}] [${level.toUpperCase()}] [${filepath}:${line}] ${message}`; // 包括文件名和行号 + } + ) + ), + maxSize: '1000m', // 每个日志文件最大尺寸 + maxFiles: '14d' // 保留的日志文件天数 +}); + +module.exports = logger \ No newline at end of file