[+] 格式化

This commit is contained in:
acgist
2023-02-04 18:29:11 +08:00
parent 96c949513c
commit 25ac02272a
4 changed files with 20 additions and 13 deletions

View File

@@ -10,8 +10,7 @@
},
"dependencies": {
"ws": "^8.12.0",
"moment": "^2.29.4",
"mediasoup": "file:./mediasoup"
},
"devDependencies": {
}
}

View File

@@ -8,6 +8,8 @@ module.exports = {
name: "taoyao-media-server",
// 交互式命令行
command: true,
// 日志级别
logLevel: 'DEBUG',
// 信令服务
https: {
listenIp: "0.0.0.0",

View File

@@ -1,12 +1,17 @@
/**
* 日志
*/
const moment = require('moment')
const config = require("./Config");
class Logger {
//
// 名称
name = config.name;
// 级别
level = [ "DEBUG", "INFO", "WARN", "ERROR", "OFF" ];
// 级别索引
levelIndex = this.level.indexOf(config.logLevel.toUpperCase());
constructor(prefix) {
if (prefix) {
@@ -15,32 +20,33 @@ class Logger {
}
debug(...args) {
this.log(console.debug, 'DEBUG', args);
return this.log(console.debug, '37m', 'DEBUG', args);
}
info(...args) {
this.log(console.info, 'INFO', args);
return this.log(console.info, '32m', 'INFO', args);
}
warn(...args) {
this.log(console.warn, 'WARN', args);
return this.log(console.warn, '33m', 'WARN', args);
}
error(...args) {
this.log(console.error, 'ERROR', args);
return this.log(console.error, '31m', 'ERROR', args);
}
log(out, level, args) {
if(!args) {
return;
log(out, color, level, args) {
if(!args || this.level.indexOf(level) < this.levelIndex) {
return this;
}
if(args.length > 1 && args[0].length > 0) {
out(`${this.name}:${level}:${args[0]}`, ...args.slice(1));
out(`\x1B[${color}${this.name} ${moment().format('yyyy-MM-DD HH:mm:ss')} : [${level.padEnd(5, ' ')}] : ${args[0]}\x1B[0m`, ...args.slice(1));
} else if(args.length === 1 && args[0].length > 0) {
out(`${this.name}:${level}:${args[0]}`);
out(`\x1B[${color}${this.name} ${moment().format('yyyy-MM-DD HH:mm:ss')} : [${level.padEnd(5, ' ')}] : ${args[0]}\x1B[0m`);
} else {
out("");
}
return this;
}
}

View File

@@ -23,7 +23,6 @@ let webSocketServer;
const mediasoupWorkers = [];
process.title = config.name;
process.env.DEBUG = process.env.DEBUG || "*mediasoup* *INFO* *WARN* *ERROR*";
/**
* 启动Mediasoup Worker
@@ -123,6 +122,7 @@ async function buildSignalServer() {
}
async function main() {
logger.debug("DEBUG").info("INFO").warn("WARN").error("ERROR");
logger.info("开始启动:%s", config.name);
// 启动Mediasoup服务
// await buildMediasoupWorkers();