Skip to content

Commit

Permalink
refactor: optimize log
Browse files Browse the repository at this point in the history
  • Loading branch information
umuoy1 committed Jan 22, 2022
1 parent d302d1f commit 95133de
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 65 deletions.
4 changes: 2 additions & 2 deletions src/engine/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Logger } from "log4js";
const FileHound = require("filehound")

import { emitter } from "@/util/utils";
import { log4js } from "../log";
import { getExtendedLogger } from "../log";
import { FileStatus } from "@/type/fileStatus";
import { roomPathStatus } from "@/engine/roomPathStatus";
import { uploadStatus } from "@/uploader/uploadStatus";
Expand Down Expand Up @@ -41,7 +41,7 @@ export class Recorder {
this.isPost = false
this._recorderTask.timeV = `${dayjs().format("YYYY-MM-DD")} ${this.getTitlePostfix()}`;

this.logger = log4js.getLogger(`Recorder ${this._recorderTask.recorderName}`)
this.logger = getExtendedLogger(`Recorder ${this._recorderTask.recorderName}`)
}

startRecord(_streamUrl: string | undefined = undefined) {
Expand Down
6 changes: 3 additions & 3 deletions src/engine/website/bilibili.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {logger} from "../../log";
import { getExtendedLogger } from "../../log";

const axios = require("axios");

Expand All @@ -11,7 +11,7 @@ export function main(url: string) {
)
.then(function (response: any) {
const data: any = response.data;
if (data["code"] != 0 || data["data"]["live_time"] < 0) {
if (data["code"] != 0 || data["data"]["live_time"] < 0) {
reject(
"BILIBILI=>No match results:Maybe the roomid is error,or this room is not open!"
);
Expand All @@ -27,7 +27,7 @@ export function main(url: string) {

axios(config).then(function (response: any) {
const html: any = response.data;
logger.trace(`获取哔哩哔哩房间 ${rid} 的推流信息 ${JSON.stringify(html, null, 2)}`)
getExtendedLogger("bilibili").trace(`获取哔哩哔哩房间 ${rid} 的推流信息 ${JSON.stringify(html, null, 2)}`)
const links: any = html["data"]["durl"];
let m3u8_url = links[0]["url"];
resolve(m3u8_url);
Expand Down
4 changes: 2 additions & 2 deletions src/http/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import axios, { AxiosInstance } from 'axios'
import * as rax from 'retry-axios';
import { log4js } from "@/log";
import { getExtendedLogger } from "@/log";
import { AxiosResponse } from 'axios';
const headers = {
'Connection': 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'User-Agent': '',
'Accept-Encoding': 'gzip,deflate',
}
const logger = log4js.getLogger(`HTTP`)
const logger = getExtendedLogger(`HTTP`)

const $axios: AxiosInstance & {
[key: string]: any
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { User } from "@/uploader/user";
import { log4js } from "@/log";
import { getExtendedLogger } from "@/log";
import { Logger } from "log4js";
import * as fs from "fs";
import { join, basename } from "path";
Expand Down Expand Up @@ -46,7 +46,7 @@ export class App {
constructor() {
global.config = this._config = require('../templates/info.json')

this._logger = log4js.getLogger(`APP`)
this._logger = getExtendedLogger(`APP`)
this._schedulers = {}
this._recorderPool = new Map<string, Recorder>()

Expand Down
41 changes: 41 additions & 0 deletions src/log/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as log4js from "log4js";

const logLevel = require('../../templates/info.json').StreamerHelper.debug ? "debug" : "info"

log4js.configure({
appenders: {
cheese: {
type: "file",
filename: process.cwd() + "/logs/artanis.log",
maxLogSize: 20971520,
backups: 10,
encoding: "utf-8",
},
memory: {
type: "file",
filename: process.cwd() + "/logs/memory.log",
maxLogSize: 20971520,
backups: 10,
encoding: "utf-8",
},
console: {
type: "console"
}
},
categories: {
cheese: {
appenders: ["cheese", "console"], level: logLevel
},
memory: {
appenders: ["memory"], level: "info"
},
check: {
appenders: ["console"], level: "debug"
},
default: {
appenders: ["cheese", "console"], level: logLevel
},
},
});

export { log4js }
91 changes: 46 additions & 45 deletions src/log/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
import * as log4js from "log4js";
const logStatus :string = require('../../templates/info.json').StreamerHelper.debug ? "debug" : "info"

log4js.configure({
appenders: {
cheese: {
type: "file",
filename: process.cwd() + "/logs/artanis.log",
maxLogSize: 20971520,
backups: 10,
encoding: "utf-8",
},
memory: {
type: "file",
filename: process.cwd() + "/logs/memory.log",
maxLogSize: 20971520,
backups: 10,
encoding: "utf-8",
},
console: {
type: "console"
import { log4js } from "./config";

export function getExtendedLogger(category?: string | undefined): log4js.Logger {
return extend(log4js.getLogger(category))
}

const ALL_VALUE = Number.MIN_VALUE,
TRACE = 5000,
DEBUG = 10000,
INFO = 20000,
WARN = 30000,
ERROR = 40000,
FATAL = 50000,
MARK = 9007199254740992,
OFF = Number.MAX_VALUE


const levels: any = {
ALL: { value: ALL_VALUE, colour: 'grey' },
TRACE: { value: TRACE, colour: 'blue' },
DEBUG: { value: DEBUG, colour: 'cyan' },
INFO: { value: INFO, colour: 'green' },
WARN: { value: WARN, colour: 'yellow' },
ERROR: { value: ERROR, colour: 'red' },
FATAL: { value: FATAL, colour: 'magenta' },
MARK: { value: MARK, colour: 'grey' }, // 2^53
OFF: { value: OFF, colour: 'grey' }
}



const extendHandler: ProxyHandler<log4js.Logger> = {
get: (obj: any, prop) => {
const propName = prop.toString().toUpperCase()
const level = levels[propName]
if (level && level.value >= WARN) {
// 推送至用户
console.log("should push tu user")
}
},
categories: {
cheese: {
appenders: ["cheese", "console"], level: logStatus
},
memory: {
appenders: ["memory"], level: "info"
},
check: {
appenders: ["console"], level: "debug"
},
default: {
appenders: ["cheese", "console"], level: logStatus
},
},
});

const logger = log4js.getLogger("cheese");
const memoryLogger = log4js.getLogger("memory");

export {
logger,
memoryLogger,
log4js

return obj[prop]

}
}

function extend(logger: log4js.Logger) {
return new Proxy<log4js.Logger>(logger, extendHandler)
}
4 changes: 2 additions & 2 deletions src/schedule/checkMemory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log4js } from "../log";
import { getExtendedLogger } from "../log";
import { Scheduler } from '@/type/scheduler'
const logger = log4js.getLogger(`memory`)
const logger = getExtendedLogger(`memory`)
import * as os from 'os';
// node进程占用的内存总量 实际堆内存的使用量 表示堆内存的总量。
const { rss, heapUsed, heapTotal } = process.memoryUsage();
Expand Down
6 changes: 3 additions & 3 deletions src/schedule/checkRoom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import * as chalk from 'chalk'

import { getStreamUrl } from "@/engine/getStreamUrl";
import { RoomStatus } from "@/engine/roomStatus";
import { log4js } from "@/log";
import { getExtendedLogger } from "@/log";
import { Scheduler } from "@/type/scheduler";
import { Recorder } from "@/engine/message";
import { RecorderTask } from "@/type/recorderTask";

const roomCheckTime = global.config.StreamerHelper.roomCheckTime
const checkTime = roomCheckTime ? roomCheckTime * 1000 : 10 * 60 * 1000
const rooms = global.config.streamerInfo;
const logger = log4js.getLogger(`checkRoom`)
const loggerCheck = log4js.getLogger(`check`)
const logger = getExtendedLogger(`checkRoom`)
const loggerCheck = getExtendedLogger(`check`)
const interval = checkTime

export default new Scheduler(interval, async function () {
Expand Down
4 changes: 2 additions & 2 deletions src/schedule/recycleFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// 定时回收文件以及处理上传失败的文件夹
import { log4js } from "@/log";
import { getExtendedLogger } from "@/log";
import * as fs from "fs";
const FileHound = require('filehound')
import { join } from 'path'
Expand All @@ -11,7 +11,7 @@ import { uploader } from "@/uploader";
import { roomPathStatus } from "@/engine/roomPathStatus";
import { Scheduler } from "@/type/scheduler";
import { RecorderTask } from "@/type/recorderTask";
const logger = log4js.getLogger(`recycleFile`);
const logger = getExtendedLogger(`recycleFile`);
const recycleCheckTime = require("../../templates/info.json").recycleCheckTime
const interval = recycleCheckTime ? recycleCheckTime * 1000 : 5 * 60 * 1000
// const interval = 1000 * 20
Expand Down
4 changes: 2 additions & 2 deletions src/uploader/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs'
import { log4js } from "../log";
import { getExtendedLogger } from "../log";
import { Logger } from "log4js";
import { localVideoPart, remoteVideoPart, uploadVideoPartInfo } from "@/type/video";
import { join } from "path";
Expand Down Expand Up @@ -42,7 +42,7 @@ export class uploader {
private succeedTotalLength: number;

constructor(recorderTask: RecorderTask) {
this.logger = log4js.getLogger(`Upload ${recorderTask.recorderName}`)
this.logger = getExtendedLogger(`Upload ${recorderTask.recorderName}`)
this.logger.debug(`Upload Stream Info ${JSON.stringify(recorderTask, null, 2)}`)
this.APPSECRET = "af125a0d5279fd576c1b4418a3e8276d"
this.dirName = recorderTask.dirName || ''
Expand Down
4 changes: 2 additions & 2 deletions src/uploader/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as terminalImage from 'terminal-image'
import { Logger } from "log4js";

import { $axios } from "../http";
import { log4js } from "../log";
import { getExtendedLogger } from "../log";
import * as crypt from '@/util/crypt'
import { BiliAPIResponse, LoginResponse, GetQRCodeResponse, GetUserInfoResponse } from "@/type/biliAPIResponse";
import { PersonInfo } from "@/type/config";
Expand Down Expand Up @@ -37,7 +37,7 @@ export class User {
this._expires_in = personInfo.expires_in;
this._nickname = personInfo.nickname;
this._tokenSignDate = personInfo.tokenSignDate;
this.logger = log4js.getLogger('User')
this.logger = getExtendedLogger('User')
this._mid = personInfo.mid || 0;
}

Expand Down

0 comments on commit 95133de

Please sign in to comment.