mirror of
https://github.com/bitwarden/browser
synced 2025-12-29 14:43:31 +00:00
log service implementation
This commit is contained in:
59
src/services/log.service.ts
Normal file
59
src/services/log.service.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import log from 'electron-log';
|
||||
import * as path from 'path';
|
||||
|
||||
import { isDev } from '../scripts/utils';
|
||||
|
||||
import { LogLevelType } from 'jslib/enums/logLevelType';
|
||||
|
||||
import { LogService as LogServiceAbstraction } from 'jslib/abstractions/log.service';
|
||||
|
||||
export class LogService implements LogServiceAbstraction {
|
||||
constructor(private filter: (level: LogLevelType) => boolean = null, logDir: string = null) {
|
||||
if (logDir != null) {
|
||||
log.transports.file.file = path.join(logDir, 'app.log');
|
||||
}
|
||||
}
|
||||
|
||||
debug(message: string) {
|
||||
if (!isDev()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.write(LogLevelType.Debug, message);
|
||||
}
|
||||
|
||||
info(message: string) {
|
||||
this.write(LogLevelType.Info, message);
|
||||
}
|
||||
|
||||
warning(message: string) {
|
||||
this.write(LogLevelType.Warning, message);
|
||||
}
|
||||
|
||||
error(message: string) {
|
||||
this.write(LogLevelType.Error, message);
|
||||
}
|
||||
|
||||
write(level: LogLevelType, message: string) {
|
||||
if (this.filter != null && this.filter(level)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (level) {
|
||||
case LogLevelType.Debug:
|
||||
log.debug(message);
|
||||
break;
|
||||
case LogLevelType.Info:
|
||||
log.info(message);
|
||||
break;
|
||||
case LogLevelType.Warning:
|
||||
log.warn(message);
|
||||
break;
|
||||
case LogLevelType.Error:
|
||||
log.error(message);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user