mirror of
https://github.com/geekgeekrun/geekgeekrun.git
synced 2026-09-08 00:47:15 +08:00
print log when launch with process.env.NODE_ENV === 'development'
This commit is contained in:
@@ -1,8 +1,15 @@
|
|||||||
|
import overrideConsole from './utils/overrideConsole'
|
||||||
import minimist from 'minimist'
|
import minimist from 'minimist'
|
||||||
import { runCommon } from './features/run-common'
|
import { runCommon } from './features/run-common'
|
||||||
import { launchDaemon } from './flow/OPEN_SETTING_WINDOW/launch-daemon'
|
import { launchDaemon } from './flow/OPEN_SETTING_WINDOW/launch-daemon'
|
||||||
import { app } from 'electron'
|
import { app } from 'electron'
|
||||||
|
|
||||||
|
const isUiDev = process.env.NODE_ENV === 'development'
|
||||||
|
const enableLogToFile = process.env.GEEKGEEKRUN_ENABLE_LOG_TO_FILE === String(1)
|
||||||
|
if (isUiDev || enableLogToFile) {
|
||||||
|
overrideConsole()
|
||||||
|
}
|
||||||
|
|
||||||
// 捕获未处理的 EPIPE 错误
|
// 捕获未处理的 EPIPE 错误
|
||||||
process.on('uncaughtException', (err) => {
|
process.on('uncaughtException', (err) => {
|
||||||
if (err?.code === 'EPIPE' || err?.code === 'ERR_STREAM_DESTROYED') {
|
if (err?.code === 'EPIPE' || err?.code === 'ERR_STREAM_DESTROYED') {
|
||||||
@@ -11,7 +18,7 @@ process.on('uncaughtException', (err) => {
|
|||||||
throw err
|
throw err
|
||||||
})
|
})
|
||||||
|
|
||||||
const isUiDev = process.env.NODE_ENV === 'development'
|
console.log(process.argv)
|
||||||
const commandlineArgs = minimist(isUiDev ? process.argv.slice(2) : process.argv.slice(1))
|
const commandlineArgs = minimist(isUiDev ? process.argv.slice(2) : process.argv.slice(1))
|
||||||
console.log(commandlineArgs)
|
console.log(commandlineArgs)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import path from 'node:path'
|
||||||
|
import os from 'node:os'
|
||||||
|
import fs from 'node:fs'
|
||||||
|
import dayjs from 'dayjs'
|
||||||
|
|
||||||
|
export default function overrideConsole() {
|
||||||
|
const originConsoleLog = console.log.bind(console)
|
||||||
|
const originConsoleWarn = console.warn.bind(console)
|
||||||
|
const originConsoleError = console.error.bind(console)
|
||||||
|
|
||||||
|
const runtimeFolderPath = path.join(os.homedir(), '.geekgeekrun')
|
||||||
|
const logDirPath = path.join(runtimeFolderPath, 'log')
|
||||||
|
if (!fs.existsSync(logDirPath)) {
|
||||||
|
fs.mkdirSync(logDirPath, { recursive: true })
|
||||||
|
}
|
||||||
|
|
||||||
|
const logFileStream = fs.createWriteStream(path.join(logDirPath, `log.log`), {
|
||||||
|
flags: 'a' // 追加模式
|
||||||
|
})
|
||||||
|
const warnFileStream = fs.createWriteStream(path.join(logDirPath, `warn.log`), {
|
||||||
|
flags: 'a' // 追加模式
|
||||||
|
})
|
||||||
|
const errorFileStream = fs.createWriteStream(path.join(logDirPath, `error.log`), {
|
||||||
|
flags: 'a' // 追加模式
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log = (...args: any[]) => {
|
||||||
|
const lineHead = `${dayjs().format('YYYY-MM-DD HH:mm:ss.SSS')} [log][PID=${process.pid}]`
|
||||||
|
originConsoleLog(lineHead, ...args)
|
||||||
|
logFileStream.write([lineHead, args.map((arg) => JSON.stringify(arg))].join(' ') + '\n')
|
||||||
|
}
|
||||||
|
console.warn = (...args: any[]) => {
|
||||||
|
const lineHead = `${dayjs().format('YYYY-MM-DD HH:mm:ss.SSS')} [warn][PID=${process.pid}]`
|
||||||
|
originConsoleWarn(lineHead, ...args)
|
||||||
|
warnFileStream.write([lineHead, args.map((arg) => JSON.stringify(arg))].join(' ') + '\n')
|
||||||
|
}
|
||||||
|
console.error = (...args: any[]) => {
|
||||||
|
const lineHead = `${dayjs().format('YYYY-MM-DD HH:mm:ss.SSS')} [warn][PID=${process.pid}]`
|
||||||
|
originConsoleError(lineHead, ...args)
|
||||||
|
errorFileStream.write([lineHead, args.map((arg) => JSON.stringify(arg))].join(' ') + '\n')
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user