forked from WhiteMinds/LiveAutoRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: http-server 支持外部传入 logger, electron 实现 logger
- Loading branch information
1 parent
8f9d078
commit 7d5f9a4
Showing
10 changed files
with
123 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import path from 'path' | ||
import { createLogger, format, transports } from 'winston' | ||
import { paths } from './env' | ||
|
||
export const logger = createLogger({ | ||
level: 'debug', | ||
format: format.combine( | ||
format.label({ label: 'default' }), | ||
format.timestamp(), | ||
format.json() | ||
), | ||
transports: [ | ||
new transports.File({ | ||
filename: path.join(paths.log, 'client-error.log'), | ||
level: 'error', | ||
}), | ||
new transports.File({ | ||
filename: path.join(paths.log, 'client-combined.log'), | ||
}), | ||
], | ||
}) | ||
|
||
// winston 的 rejectionHandlers / exceptionHandlers 实现有 bug,配置后在遇到 | ||
// unhandledRejection 时会导致 logger 的 stream 永久 pause,所以这里手动写日志。 | ||
process.on('unhandledRejection', (error) => { | ||
logger.error('unhandledRejection', error) | ||
}) | ||
process.on('unhandleExceptions', (error) => { | ||
logger.error('unhandleExceptions', error) | ||
}) | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
logger.add( | ||
new transports.Console({ | ||
format: format.combine( | ||
format.colorize(), | ||
format.align(), | ||
format.printf(({ level, message, label, timestamp }) => { | ||
return `${timestamp} [${label}] ${level}: ${message}` | ||
}) | ||
), | ||
}) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,49 @@ | ||
import path from 'path' | ||
import { createLogger, format, transports } from 'winston' | ||
import { | ||
createLogger as createWinstonLogger, | ||
format, | ||
transports, | ||
} from 'winston' | ||
import { paths } from './env' | ||
|
||
export const logger = createLogger({ | ||
level: 'debug', | ||
format: format.combine( | ||
format.label({ label: 'default' }), | ||
format.timestamp(), | ||
format.json() | ||
), | ||
transports: [ | ||
new transports.File({ | ||
filename: path.join(paths.log, 'error.log'), | ||
level: 'error', | ||
}), | ||
new transports.File({ filename: path.join(paths.log, 'combined.log') }), | ||
], | ||
}) | ||
export function createLogger() { | ||
const logger = createWinstonLogger({ | ||
levels: { | ||
error: 3, | ||
warn: 4, | ||
info: 6, | ||
debug: 7, | ||
}, | ||
level: 'debug', | ||
format: format.combine( | ||
format.label({ label: 'default' }), | ||
format.timestamp(), | ||
format.json() | ||
), | ||
transports: [ | ||
new transports.File({ | ||
filename: path.join(paths.log, 'server-error.log'), | ||
level: 'error', | ||
}), | ||
new transports.File({ | ||
filename: path.join(paths.log, 'server-combined.log'), | ||
}), | ||
], | ||
}) | ||
|
||
// winston 的 rejectionHandlers / exceptionHandlers 实现有 bug,配置后在遇到 | ||
// unhandledRejection 时会导致 logger 的 stream 永久 pause,所以这里手动写日志。 | ||
process.on('unhandledRejection', (error) => { | ||
logger.error('unhandledRejection', error) | ||
}) | ||
process.on('unhandleExceptions', (error) => { | ||
logger.error('unhandleExceptions', error) | ||
}) | ||
if (process.env.NODE_ENV !== 'production') { | ||
logger.add( | ||
new transports.Console({ | ||
format: format.combine( | ||
format.colorize(), | ||
format.align(), | ||
format.printf(({ level, message, label, timestamp }) => { | ||
return `${timestamp} [${label}] ${level}: ${message}` | ||
}) | ||
), | ||
}) | ||
) | ||
} | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
logger.add( | ||
new transports.Console({ | ||
format: format.combine( | ||
format.colorize(), | ||
format.align(), | ||
format.printf(({ level, message, label, timestamp }) => { | ||
return `${timestamp} [${label}] ${level}: ${message}` | ||
}) | ||
), | ||
}) | ||
) | ||
return logger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { Settings } from '@autorecord/shared' | ||
|
||
export type LogFn = (data: unknown) => void | ||
|
||
export interface ServerOpts { | ||
getSettings: () => Promise<Settings> | ||
setSettings: (newSettings: Settings) => Promise<Settings> | ||
logger: { | ||
error: LogFn | ||
warn: LogFn | ||
info: LogFn | ||
debug: LogFn | ||
} | ||
ffmpegPath?: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters