xref: /MusicFree/src/utils/log.ts (revision ea6d708f22409666b768cda1107ea1066b213247)
1242960d3S猫头猫import {logger, fileAsyncTransport} from 'react-native-logs';
2242960d3S猫头猫import RNFS from 'react-native-fs';
3242960d3S猫头猫import pathConst from '@/constants/pathConst';
4e22d5e4fS猫头猫import Config from '../core/config';
5*ea6d708fS猫头猫import {addLog} from '@/lib/react-native-vdebug/src/log';
6242960d3S猫头猫
7242960d3S猫头猫const config = {
8242960d3S猫头猫    transport: fileAsyncTransport,
9242960d3S猫头猫    transportOptions: {
10242960d3S猫头猫        FS: RNFS,
11242960d3S猫头猫        filePath: pathConst.logPath,
12242960d3S猫头猫        fileName: `error-log-{date-today}.log`,
13242960d3S猫头猫    },
14242960d3S猫头猫    dateFormat: 'local',
15242960d3S猫头猫};
16242960d3S猫头猫
17242960d3S猫头猫const traceConfig = {
18242960d3S猫头猫    transport: fileAsyncTransport,
19242960d3S猫头猫    transportOptions: {
20242960d3S猫头猫        FS: RNFS,
21242960d3S猫头猫        filePath: pathConst.logPath,
22242960d3S猫头猫        fileName: `trace-log.log`,
23242960d3S猫头猫    },
24242960d3S猫头猫    dateFormat: 'local',
25242960d3S猫头猫};
26242960d3S猫头猫
27242960d3S猫头猫const log = logger.createLogger(config);
28242960d3S猫头猫const traceLogger = logger.createLogger(traceConfig);
29242960d3S猫头猫
30242960d3S猫头猫export function trace(
31242960d3S猫头猫    desc: string,
321e263108S猫头猫    message?: any,
33242960d3S猫头猫    level: 'info' | 'error' = 'info',
34242960d3S猫头猫) {
35242960d3S猫头猫    if (__DEV__) {
36242960d3S猫头猫        console.log(desc, message);
37242960d3S猫头猫    }
38242960d3S猫头猫    // 特殊情况记录操作路径
39e22d5e4fS猫头猫    if (Config.get('setting.basic.debug.traceLog')) {
40242960d3S猫头猫        traceLogger[level]({
41242960d3S猫头猫            desc,
42242960d3S猫头猫            message,
43242960d3S猫头猫        });
44242960d3S猫头猫    }
45242960d3S猫头猫}
46242960d3S猫头猫
47242960d3S猫头猫export function errorLog(desc: string, message: any) {
48e22d5e4fS猫头猫    if (Config.get('setting.basic.debug.errorLog')) {
49242960d3S猫头猫        log.error({
50242960d3S猫头猫            desc,
51242960d3S猫头猫            message,
52242960d3S猫头猫        });
53242960d3S猫头猫        trace(desc, message, 'error');
54242960d3S猫头猫    }
55242960d3S猫头猫}
56242960d3S猫头猫
57*ea6d708fS猫头猫export function devLog(
58*ea6d708fS猫头猫    method: 'log' | 'error' | 'warn' | 'info',
59*ea6d708fS猫头猫    ...args: any[]
60*ea6d708fS猫头猫) {
61*ea6d708fS猫头猫    if (Config.get('setting.basic.debug.devLog')) {
62*ea6d708fS猫头猫        addLog(method, args);
63*ea6d708fS猫头猫    }
64*ea6d708fS猫头猫}
65*ea6d708fS猫头猫
66242960d3S猫头猫export {log};
67