Skip to content

Commit

Permalink
refactor: 增加日志压缩、自动删除日志、日志级别设置
Browse files Browse the repository at this point in the history
  • Loading branch information
CaoMeiYouRen committed Nov 1, 2024
1 parent b7b3928 commit c3d378b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const TZ = env.TZ || 'Asia/Shanghai'

export const TIMEOUT = Number(env.TIMEOUT || 10 * 1000)

export const LOG_LEVEL = process.env.LOG_LEVEL || (__DEV__ ? 'silly' : 'http')

// 每页最大查询条数
export const PAGE_LIMIT_MAX = Number(env.PAGE_LIMIT_MAX || 1000)

Expand Down
8 changes: 4 additions & 4 deletions src/middlewares/logger.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { isNumberString } from 'class-validator'
import { Logger, QueryRunner } from 'typeorm'
import { LoggerService } from '@nestjs/common'
import { timeFormat } from '@/utils/helper'
import { __DEV__, __PROD__ } from '@/app.config'
import { __DEV__, __PROD__, LOG_LEVEL } from '@/app.config'

Check warning on line 11 in src/middlewares/logger.middleware.ts

View workflow job for this annotation

GitHub Actions / Test

'__DEV__' is defined but never used

Check warning on line 11 in src/middlewares/logger.middleware.ts

View workflow job for this annotation

GitHub Actions / Test

'__DEV__' is defined but never used
import { User } from '@/db/models/user.entity'

const logDir = path.resolve('logs')
export const logDir = path.resolve('logs')

morgan.token('user', (req: Request) => {
const user = req.user as User
Expand Down Expand Up @@ -78,15 +78,15 @@ const format = winston.format.combine(
const dailyRotateFileOption = {
dirname: logDir,
datePattern: 'YYYY-MM-DD',
zippedArchive: false,
zippedArchive: __PROD__, // 如果是生产环境,压缩日志文件
maxSize: '20m',
maxFiles: '31d',
format,
auditFile: path.join(logDir, '.audit.json'),
}

export const winstonLogger = WinstonModule.createLogger({
level: __DEV__ ? 'silly' : 'http',
level: LOG_LEVEL,
exitOnError: false,
transports: [
new winston.transports.Console({
Expand Down
26 changes: 26 additions & 0 deletions src/services/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { AIConfig } from '@/models/ai-config'
import { HttpError } from '@/models/http-error'
import { RegularConfig } from '@/models/regular-config'
import { DailyCount } from '@/db/models/daily-count.entity'
import { logDir } from '@/middlewares/logger.middleware'

const removeQueue = new PQueue({ concurrency: Math.min(os.cpus().length, 8) }) // 删除文件并发数
const rssQueue = new PQueue({ concurrency: RSS_LIMIT_MAX }) // RSS 请求并发数
Expand Down Expand Up @@ -1359,6 +1360,31 @@ EXAMPLE JSON ERROR OUTPUT:
}
}

@Cron(CronExpression.EVERY_DAY_AT_4AM, { name: 'removeLogs' }) // 每天删除一次
private async removeLogFiles() {
try {
const dirPath = logDir// 解析为绝对路径
const files = await fs.readdir(dirPath)

files.forEach((file) => {
if (/\.(log(.gz)?)$/.test(file)) {
return null
}
return removeQueue.add(async () => {
// 检查日志最后写入时间是否超过31天
const stats = await fs.stat(path.join(dirPath, file))
const date = stats.mtime
const days = dayjs().diff(date, 'day')
if (days > 31) {
await fs.remove(path.join(dirPath, file))
}
})
})
} catch (error) {
this.logger.error(error?.message, error?.stack)
}
}

private async dailyCountByDate(dateInput: string | Date | Dayjs) { // 'YYYY-MM-DD'
const defaultDate = dayjs(dateInput).tz().hour(0).minute(0).second(0).millisecond(0)
const start = defaultDate.toDate() // 从0点开始算
Expand Down

0 comments on commit c3d378b

Please sign in to comment.