@intuitionrobotics/ts-common
Version:
81 lines • 2.6 kB
JavaScript
/*
* ts-common is the basic building blocks of our typescript projects
*
* Copyright (C) 2020 Intuition Robotics
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DebugFlag, DebugFlags } from "../debug-flags.js";
import { LogLevel, LogLevelOrdinal } from "./types.js";
import { BeLogged } from "./BeLogged.js";
export class Logger {
static log = new Logger("LOGGER");
tag;
static defaultFlagState = true;
_DEBUG_FLAG;
minLevel = LogLevel.Verbose;
constructor(tag) {
this.tag = tag ?? this.constructor["name"];
this._DEBUG_FLAG = DebugFlags.createFlag(this.tag);
this._DEBUG_FLAG.enable(Logger.defaultFlagState);
}
setMinLevel(minLevel) {
this.minLevel = minLevel;
}
setTag(tag) {
this.tag = tag;
this._DEBUG_FLAG.rename(tag);
}
logVerbose(...toLog) {
this.log(LogLevel.Verbose, false, toLog);
}
logDebug(...toLog) {
this.log(LogLevel.Debug, false, toLog);
}
logInfo(...toLog) {
this.log(LogLevel.Info, false, toLog);
}
logWarning(...toLog) {
this.log(LogLevel.Warning, false, toLog);
}
logError(...toLog) {
this.log(LogLevel.Error, false, toLog);
}
logVerboseBold(...toLog) {
this.log(LogLevel.Verbose, true, toLog);
}
logDebugBold(...toLog) {
this.log(LogLevel.Debug, true, toLog);
}
logInfoBold(...toLog) {
this.log(LogLevel.Info, true, toLog);
}
logWarningBold(...toLog) {
this.log(LogLevel.Warning, true, toLog);
}
logErrorBold(...toLog) {
this.log(LogLevel.Error, true, toLog);
}
log(level, bold, toLog) {
if (!this.assertCanPrint(level))
return;
// @ts-expect-error TS struggles with this dynamic typing
BeLogged.logImpl(this.tag, level, bold, toLog);
}
assertCanPrint(level) {
if (!this._DEBUG_FLAG.isEnabled())
return;
return LogLevelOrdinal.indexOf(level) >= LogLevelOrdinal.indexOf(this.minLevel);
}
}
//# sourceMappingURL=Logger.js.map