@nsalaun/ng-logger
Version:
Angular logger service
197 lines (190 loc) • 6.25 kB
JavaScript
import * as i0 from '@angular/core';
import { InjectionToken, Injectable, Inject, NgModule } from '@angular/core';
/**
* @license
* Copyright Noémi Salaün All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/noemi-salaun/ng-logger/blob/master/LICENSE
*/
/**
* Created by Noémi Salaün on 09/17/2016.
*/
/**
* The available log levels.
*/
var Level;
(function (Level) {
Level[Level["OFF"] = 0] = "OFF";
Level[Level["ERROR"] = 1] = "ERROR";
Level[Level["WARN"] = 2] = "WARN";
Level[Level["INFO"] = 3] = "INFO";
Level[Level["DEBUG"] = 4] = "DEBUG";
Level[Level["LOG"] = 5] = "LOG";
})(Level || (Level = {}));
/**
* @license
* Copyright Noémi Salaün All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/noemi-salaun/ng-logger/blob/master/LICENSE
*/
/**
* Created by Noémi Salaün on 09/17/2016.
*/
/**
* The token to provide the log {@link Level}.
*/
const LOGGER_LEVEL = new InjectionToken('LoggerLevel');
/**
* A logger service that provide the same functions as {@link console}.
* The logger is binded to the console, so the Web Console shows the correct file and line number of the original call.
*/
class Logger {
constructor(level) {
this._level = level;
// console.log
if (this._level >= Level.LOG && console && console.log) {
this.log = console.log.bind(console);
}
else {
this.log = () => {
// Nothing
};
}
// console.debug
if (this._level >= Level.DEBUG && console && console.debug) {
this.debug = console.debug.bind(console);
}
else {
this.debug = () => {
// Nothing
};
}
// console.info
if (this._level >= Level.INFO && console && console.info) {
this.info = console.info.bind(console);
}
else {
this.info = () => {
// Nothing
};
}
// console.warn
if (this._level >= Level.WARN && console && console.warn) {
this.warn = console.warn.bind(console);
}
else {
this.warn = () => {
// Nothing
};
}
// console.error
if (this._level >= Level.ERROR && console && console.error) {
this.error = console.error.bind(console);
}
else {
this.error = () => {
// Nothing
};
}
// console.group
if (this._level > Level.OFF && console && console.group) {
this.group = console.group.bind(console);
}
else {
this.group = () => {
// Nothing
};
}
// console.groupCollapsed
if (this._level > Level.OFF && console && console.groupCollapsed) {
this.groupCollapsed = console.groupCollapsed.bind(console);
}
else {
this.groupCollapsed = () => {
// Nothing
};
}
// console.groupEnd
if (this._level > Level.OFF && console && console.groupEnd) {
this.groupEnd = console.groupEnd.bind(console);
}
else {
this.groupEnd = () => {
// Nothing
};
}
// console.time
if (this._level >= Level.DEBUG && console && console.time) {
this.time = console.time.bind(console);
}
else {
this.time = () => {
// Nothing
};
}
// console.timeEnd
if (this._level >= Level.DEBUG && console && console.timeEnd) {
this.timeEnd = console.timeEnd.bind(console);
}
else {
this.timeEnd = () => {
// Nothing
};
}
}
/**
* Returns the log level.
*/
get level() {
return this._level;
}
}
Logger.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: Logger, deps: [{ token: LOGGER_LEVEL }], target: i0.ɵɵFactoryTarget.Injectable });
Logger.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: Logger });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: Logger, decorators: [{
type: Injectable
}], ctorParameters: function () { return [{ type: Level, decorators: [{
type: Inject,
args: [LOGGER_LEVEL]
}] }]; } });
/**
* @license
* Copyright Noémi Salaün All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/noemi-salaun/ng-logger/blob/master/LICENSE
*/
/**
* Created by Noémi Salaün on 09/17/2016.
*/
class NgLoggerModule {
/**
* Provide the {@link Logger} with the given log {@link Level}.
* @param level The log level.
*/
static forRoot(level = Level.LOG) {
return {
ngModule: NgLoggerModule,
providers: [
Logger,
{ provide: LOGGER_LEVEL, useValue: level },
],
};
}
}
NgLoggerModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgLoggerModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
NgLoggerModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgLoggerModule });
NgLoggerModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgLoggerModule });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: NgLoggerModule, decorators: [{
type: NgModule
}] });
/*
* Public API Surface of ng-logger
*/
/**
* Generated bundle index. Do not edit.
*/
export { LOGGER_LEVEL, Level, Logger, NgLoggerModule };
//# sourceMappingURL=nsalaun-ng-logger.js.map