mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-20 09:43:59 +00:00
feat(packages/shared): add logger factory
- @revanced/websocket-api now also utilizes the new logger from the shared package - @revanced/websocket-api/utils/checkEnv has been renamed to its full form - It also no longer returns anything as it's no longer needed
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
import {
|
||||
ClientOperation,
|
||||
Operation,
|
||||
ServerOperation,
|
||||
} from '../constants/Operation.js'
|
||||
import { ClientOperation, Operation, ServerOperation } from '../constants/Operation.js'
|
||||
import { Packet } from '../schemas/Packet.js'
|
||||
|
||||
/**
|
||||
@@ -11,10 +7,7 @@ import { Packet } from '../schemas/Packet.js'
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is trying to do the operation given
|
||||
*/
|
||||
export function packetMatchesOperation<TOp extends Operation>(
|
||||
op: TOp,
|
||||
packet: Packet,
|
||||
): packet is Packet<TOp> {
|
||||
export function packetMatchesOperation<TOp extends Operation>(op: TOp, packet: Packet): packet is Packet<TOp> {
|
||||
return packet.op === op
|
||||
}
|
||||
|
||||
@@ -23,9 +16,7 @@ export function packetMatchesOperation<TOp extends Operation>(
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is a client packet
|
||||
*/
|
||||
export function isClientPacket(
|
||||
packet: Packet,
|
||||
): packet is Packet<ClientOperation> {
|
||||
export function isClientPacket(packet: Packet): packet is Packet<ClientOperation> {
|
||||
return packet.op in ClientOperation
|
||||
}
|
||||
|
||||
@@ -34,8 +25,6 @@ export function isClientPacket(
|
||||
* @param packet A packet
|
||||
* @returns Whether this packet is a server packet
|
||||
*/
|
||||
export function isServerPacket(
|
||||
packet: Packet,
|
||||
): packet is Packet<ServerOperation> {
|
||||
export function isServerPacket(packet: Packet): packet is Packet<ServerOperation> {
|
||||
return packet.op in ServerOperation
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './guard.js'
|
||||
export * from './logger.js'
|
||||
export * from './serialization.js'
|
||||
export * from './string.js'
|
||||
|
||||
66
packages/shared/src/utils/logger.ts
Normal file
66
packages/shared/src/utils/logger.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { createLogger as createWinstonLogger, LoggerOptions, transports, format } from 'winston'
|
||||
import { Chalk, ChalkInstance } from 'chalk'
|
||||
|
||||
const chalk = new Chalk()
|
||||
|
||||
const LevelPrefixes = {
|
||||
error: `${chalk.bgRed.whiteBright(' ERR! ')} `,
|
||||
warn: `${chalk.bgYellow.black(' WARN ')} `,
|
||||
info: `${chalk.bgBlue.whiteBright(' INFO ')} `,
|
||||
log: chalk.reset(''),
|
||||
debug: chalk.gray('DEBUG: '),
|
||||
silly: chalk.gray('SILLY: '),
|
||||
} as Record<string, string>
|
||||
|
||||
const LevelColorFunctions = {
|
||||
error: chalk.redBright,
|
||||
warn: chalk.yellowBright,
|
||||
info: chalk.cyanBright,
|
||||
log: chalk.reset,
|
||||
debug: chalk.gray,
|
||||
silly: chalk.gray,
|
||||
} as Record<string, ChalkInstance>
|
||||
|
||||
export function createLogger(
|
||||
serviceName: string,
|
||||
config: SafeOmit<
|
||||
LoggerOptions,
|
||||
| 'defaultMeta'
|
||||
| 'exceptionHandlers'
|
||||
| 'exitOnError'
|
||||
| 'handleExceptions'
|
||||
| 'handleRejections'
|
||||
| 'levels'
|
||||
| 'rejectionHandlers'
|
||||
>,
|
||||
) {
|
||||
const logger = createWinstonLogger({
|
||||
exitOnError: false,
|
||||
defaultMeta: { serviceName },
|
||||
handleExceptions: true,
|
||||
handleRejections: true,
|
||||
transports: config.transports ?? [
|
||||
new transports.Console(),
|
||||
new transports.File({
|
||||
dirname: 'logs',
|
||||
filename: `${serviceName}-${Date.now()}.log`,
|
||||
format: format.combine(
|
||||
format.uncolorize(),
|
||||
format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
||||
format.printf(
|
||||
({ level, message, timestamp }) => `[${timestamp}] ${level.toUpperCase()}: ${message}`,
|
||||
),
|
||||
),
|
||||
}),
|
||||
],
|
||||
format: format.printf(({ level, message }) => LevelPrefixes[level] + LevelColorFunctions[level]!(message)),
|
||||
...config,
|
||||
})
|
||||
|
||||
logger.silly(`Logger for ${serviceName} created at ${Date.now()}`)
|
||||
|
||||
return logger
|
||||
}
|
||||
|
||||
type SafeOmit<T, K extends keyof T> = Omit<T, K>
|
||||
export type Logger = ReturnType<typeof createLogger>
|
||||
Reference in New Issue
Block a user