ente/packages/shared/logging/index.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

import isElectron from 'is-electron';
import { logError } from '@ente/shared/sentry';
import { getAppEnv } from '../apps/env';
import { APP_ENV } from '../apps/constants';
import { formatLog, logWeb } from './web';
2024-01-03 07:03:53 +00:00
import { WorkerSafeElectronService } from '../electron/service';
export const MAX_LOG_SIZE = 5 * 1024 * 1024; // 5MB
export const MAX_LOG_LINES = 1000;
export function addLogLine(
log: string | number | boolean,
...optionalParams: (string | number | boolean)[]
) {
try {
const completeLog = [log, ...optionalParams].join(' ');
if (getAppEnv() === APP_ENV.DEVELOPMENT) {
console.log(completeLog);
}
if (isElectron()) {
2024-01-03 07:03:53 +00:00
WorkerSafeElectronService.logToDisk(completeLog);
} else {
logWeb(completeLog);
}
} catch (e) {
logError(e, 'failed to addLogLine', undefined, true);
// ignore
}
}
export const addLocalLog = (getLog: () => string) => {
if (getAppEnv() === APP_ENV.DEVELOPMENT) {
console.log(
formatLog({
logLine: getLog(),
timestamp: Date.now(),
})
);
}
};