ing
This commit is contained in:
commit
12e77ec0fa
46
.gitignore
vendored
Normal file
46
.gitignore
vendored
Normal file
|
@ -0,0 +1,46 @@
|
|||
# See http://help.github.com/ignore-files/ for more about ignoring files.
|
||||
|
||||
# compiled output
|
||||
/dist
|
||||
/dist-server
|
||||
/tmp
|
||||
/out-tsc
|
||||
/docs
|
||||
|
||||
# dependencies
|
||||
/node_modules
|
||||
|
||||
# IDEs and editors
|
||||
/.idea
|
||||
.project
|
||||
.classpath
|
||||
.c9/
|
||||
*.launch
|
||||
.settings/
|
||||
*.sublime-workspace
|
||||
|
||||
# IDE - VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
|
||||
# misc
|
||||
/.sass-cache
|
||||
/connect.lock
|
||||
/coverage
|
||||
/libpeerconnection.log
|
||||
npm-debug.log
|
||||
testem.log
|
||||
/typings
|
||||
|
||||
# e2e
|
||||
/e2e/*.js
|
||||
/e2e/*.map
|
||||
|
||||
# System Files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
yarn.lock
|
9
.make-package.js
Normal file
9
.make-package.js
Normal file
|
@ -0,0 +1,9 @@
|
|||
const fs = require('fs');
|
||||
const packageJson = JSON.parse(fs.readFileSync('./package.json').toString());
|
||||
|
||||
delete packageJson.devDependencies;
|
||||
delete packageJson.scripts;
|
||||
|
||||
packageJson.private = false;
|
||||
|
||||
fs.writeFileSync('./dist/package.json', JSON.stringify(packageJson, null, 2));
|
21
License
Normal file
21
License
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2017
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
6492
package-lock.json
generated
Normal file
6492
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
43
package.json
Normal file
43
package.json
Normal file
|
@ -0,0 +1,43 @@
|
|||
{
|
||||
"name": "@loafer/ng-logger",
|
||||
"description": "Angular Logger",
|
||||
"version": "0.0.1",
|
||||
"license": "MIT",
|
||||
"private": true,
|
||||
"main": "./index.js",
|
||||
"typings": "./index.d.ts",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://git.loafle.net/loafer/typescript/ng-logger.git"
|
||||
},
|
||||
"publishConfig": {
|
||||
"registry": "https://nexus.loafle.net/repository/npm-loafle/"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Loafle <loafer@loafle.com>",
|
||||
"scripts": {
|
||||
"generate:package": "node .make-package.js",
|
||||
"generate:doc": "rimraf docs && typedoc --out docs --target es6 --theme minimal --mode file src",
|
||||
"bundle": "rimraf dist && tsc",
|
||||
"build": "npm run bundle && npm run generate:package && npm run generate:doc"
|
||||
},
|
||||
"dependencies": {
|
||||
"@angular/common": "^5.2.9",
|
||||
"@angular/core": "^5.2.9",
|
||||
"rxjs": "^5.5.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^22.2.3",
|
||||
"@types/node": "^9.3.0",
|
||||
"jest": "^22.4.3",
|
||||
"prettier": "^1.12.1",
|
||||
"rimraf": "^2.6.2",
|
||||
"ts-jest": "^22.4.6",
|
||||
"ts-node": "^6.0.3",
|
||||
"tslint": "^5.10.0",
|
||||
"tslint-config-prettier": "^1.12.0",
|
||||
"tslint-config-standard": "^7.0.0",
|
||||
"typedoc": "^0.11.1",
|
||||
"typescript": "^2.8.3"
|
||||
}
|
||||
}
|
6
src/core/config.ts
Normal file
6
src/core/config.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { LoggerLevel } from './type';
|
||||
|
||||
export interface LoggerConfig {
|
||||
level: LoggerLevel;
|
||||
serverLogLevel?: LoggerLevel;
|
||||
}
|
3
src/core/index.ts
Normal file
3
src/core/index.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
export * from './config';
|
||||
export * from './token';
|
||||
export * from './type';
|
3
src/core/token.ts
Normal file
3
src/core/token.ts
Normal file
|
@ -0,0 +1,3 @@
|
|||
import { InjectionToken } from '@angular/core';
|
||||
|
||||
export const _LOGGER_CONFIG = new InjectionToken('@loafer/ng-logger Internal Logger config');
|
26
src/core/type.ts
Normal file
26
src/core/type.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
export enum LoggerLevel {
|
||||
TRACE = 0,
|
||||
DEBUG,
|
||||
INFO,
|
||||
LOG,
|
||||
WARN,
|
||||
ERROR,
|
||||
OFF,
|
||||
}
|
||||
|
||||
export const LoggerLevelName = [
|
||||
'TRACE',
|
||||
'DEBUG',
|
||||
'INFO',
|
||||
'LOG',
|
||||
'WARN',
|
||||
'ERROR',
|
||||
'OFF'
|
||||
];
|
||||
|
||||
export interface ServerLoggingParameter {
|
||||
level: string;
|
||||
message: string;
|
||||
addtional?: string;
|
||||
timestamp: Date;
|
||||
}
|
1
src/index.ts
Normal file
1
src/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export * from './ng-logger.module';
|
62
src/ng-logger.module.ts
Normal file
62
src/ng-logger.module.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import {
|
||||
NgModule,
|
||||
ModuleWithProviders,
|
||||
} from '@angular/core';
|
||||
|
||||
import {
|
||||
_LOGGER_CONFIG,
|
||||
LoggerConfig,
|
||||
} from './core';
|
||||
|
||||
import {
|
||||
SERVICES, LoggerService,
|
||||
} from './service';
|
||||
|
||||
export interface LoggerFeatureModuleConfig {
|
||||
url?: any;
|
||||
}
|
||||
|
||||
export interface LoggerRootModuleConfig {
|
||||
config: LoggerConfig;
|
||||
}
|
||||
|
||||
@NgModule({})
|
||||
export class LoggerRootModule {
|
||||
constructor(
|
||||
private loggerService: LoggerService,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule({})
|
||||
export class LoggerFeatureModule {
|
||||
constructor(
|
||||
private loggerService: LoggerService,
|
||||
private root: LoggerRootModule,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@NgModule({})
|
||||
export class LoggerModule {
|
||||
static forRoot(config: LoggerRootModuleConfig): ModuleWithProviders {
|
||||
return {
|
||||
ngModule: LoggerRootModule,
|
||||
providers: [
|
||||
{
|
||||
provide: _LOGGER_CONFIG,
|
||||
useValue: config.config,
|
||||
},
|
||||
SERVICES,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
static forFeature(config: LoggerFeatureModuleConfig): ModuleWithProviders {
|
||||
return {
|
||||
ngModule: LoggerFeatureModule,
|
||||
providers: [
|
||||
],
|
||||
};
|
||||
}
|
||||
}
|
7
src/service/index.ts
Normal file
7
src/service/index.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
export * from './logger.service';
|
||||
|
||||
import { LoggerService } from './logger.service';
|
||||
|
||||
export const SERVICES = [
|
||||
LoggerService,
|
||||
];
|
158
src/service/logger.service.ts
Normal file
158
src/service/logger.service.ts
Normal file
|
@ -0,0 +1,158 @@
|
|||
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
|
||||
import { isPlatformBrowser } from '@angular/common';
|
||||
|
||||
import {
|
||||
LoggerConfig,
|
||||
LoggerLevel,
|
||||
LoggerLevelName,
|
||||
_LOGGER_CONFIG,
|
||||
} from '../core';
|
||||
|
||||
export type ConsoleFunc = (message?: any, ...optionalParams: any[]) => void;
|
||||
|
||||
@Injectable()
|
||||
export class LoggerService {
|
||||
private _isIE: boolean;
|
||||
|
||||
public constructor(
|
||||
@Inject(_LOGGER_CONFIG) private readonly config: LoggerConfig,
|
||||
@Inject(PLATFORM_ID) private readonly platformId,
|
||||
) {
|
||||
this._isIE = isPlatformBrowser(platformId) &&
|
||||
!!(navigator.userAgent.indexOf('MSIE') !== -1 || navigator.userAgent.match(/Trident\//) || navigator.userAgent.match(/Edge\//));
|
||||
|
||||
}
|
||||
|
||||
public get trace(): ConsoleFunc {
|
||||
return this.getConsoleMethod(LoggerLevel.TRACE);
|
||||
}
|
||||
public get debug(): ConsoleFunc {
|
||||
return this.getConsoleMethod(LoggerLevel.DEBUG);
|
||||
}
|
||||
public get info(): ConsoleFunc {
|
||||
return this.getConsoleMethod(LoggerLevel.INFO);
|
||||
}
|
||||
public get log(): ConsoleFunc {
|
||||
return this.getConsoleMethod(LoggerLevel.LOG);
|
||||
}
|
||||
public get warn(): ConsoleFunc {
|
||||
return this.getConsoleMethod(LoggerLevel.WARN);
|
||||
}
|
||||
public get error(): ConsoleFunc {
|
||||
return this.getConsoleMethod(LoggerLevel.ERROR);
|
||||
}
|
||||
|
||||
private _console_log: ConsoleFunc;
|
||||
|
||||
private getConsoleMethod(level: LoggerLevel): ConsoleFunc {
|
||||
if (level < this.config.level) {
|
||||
return (message, ...additional: any[]) => {};
|
||||
}
|
||||
|
||||
if (this._isIE) {
|
||||
switch (level) {
|
||||
case LoggerLevel.WARN:
|
||||
return console.warn.bind(console, ...this.getLogHeader(level));
|
||||
case LoggerLevel.ERROR:
|
||||
return console.error.bind(console, ...this.getLogHeader(level));
|
||||
case LoggerLevel.INFO:
|
||||
return console.info.bind(console, ...this.getLogHeader(level));
|
||||
default:
|
||||
return console.log.bind(console, ...this.getLogHeader(level));
|
||||
}
|
||||
} else {
|
||||
return console.log.bind(console, ...this.getLogHeader(level));
|
||||
}
|
||||
}
|
||||
|
||||
private getLogHeader(level: LoggerLevel): any[] {
|
||||
const params: any[] = [];
|
||||
params.push(`%c${this._timestamp()} [${LoggerLevelName[level]}]`);
|
||||
if (!this._isIE) {
|
||||
const color = this._getColor(level);
|
||||
params.push(`color:${color}`);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
private _timestamp(): string {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
private _log(level: LoggerLevel, message, additional: any[] = []): void {
|
||||
if (!message) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Allow logging on server even if client log level is off
|
||||
// if (logOnServer) {
|
||||
// this._logOnServer(level, message, additional);
|
||||
// }
|
||||
|
||||
// if no message or the log level is less than the environ
|
||||
if (level < this.config.level) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
message = typeof message === 'string' ? message : JSON.stringify(message, null, 2);
|
||||
} catch (e) {
|
||||
additional = [message, ...additional];
|
||||
message = 'The provided "message" value could not be parsed with JSON.stringify().';
|
||||
}
|
||||
|
||||
// Coloring doesn't work in IE
|
||||
if (this._isIE) {
|
||||
return this._logIE(level, message, additional);
|
||||
}
|
||||
|
||||
const color = this._getColor(level);
|
||||
|
||||
const params: any[] = [];
|
||||
params.push(`%c${this._timestamp()} [${LoggerLevelName[level]}]`);
|
||||
params.push(`color:${color}`);
|
||||
params.push(message);
|
||||
params.push(...additional);
|
||||
|
||||
console.log.apply(console, params);
|
||||
}
|
||||
|
||||
private _logIE(level: LoggerLevel, message: string, additional: any[] = []): void {
|
||||
const params: any[] = [];
|
||||
params.push(`${this._timestamp()} [${LoggerLevelName[level]}] `);
|
||||
params.push(message);
|
||||
params.push(...additional);
|
||||
|
||||
switch (level) {
|
||||
case LoggerLevel.WARN:
|
||||
console.warn.apply(console, params);
|
||||
break;
|
||||
case LoggerLevel.ERROR:
|
||||
console.error.apply(console, params);
|
||||
break;
|
||||
case LoggerLevel.INFO:
|
||||
console.info.apply(console, params);
|
||||
break;
|
||||
default:
|
||||
console.log.apply(console, params);
|
||||
}
|
||||
}
|
||||
|
||||
private _getColor(level: LoggerLevel): 'blue' | 'teal' | 'gray' | 'red' | undefined {
|
||||
switch (level) {
|
||||
case LoggerLevel.TRACE:
|
||||
return 'blue';
|
||||
case LoggerLevel.DEBUG:
|
||||
return 'teal';
|
||||
case LoggerLevel.INFO:
|
||||
case LoggerLevel.LOG:
|
||||
return 'gray';
|
||||
case LoggerLevel.WARN:
|
||||
case LoggerLevel.ERROR:
|
||||
return 'red';
|
||||
case LoggerLevel.OFF:
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
45
tsconfig.json
Normal file
45
tsconfig.json
Normal file
|
@ -0,0 +1,45 @@
|
|||
{
|
||||
"compileOnSave": false,
|
||||
"compilerOptions": {
|
||||
"baseUrl": "./src",
|
||||
"outDir": "./dist",
|
||||
"sourceMap": true,
|
||||
"declaration": true,
|
||||
"newLine": "LF",
|
||||
"moduleResolution": "node",
|
||||
|
||||
/* Strict Type-Checking Options */
|
||||
// "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
|
||||
// "strictNullChecks": true /* Enable strict null checks. */,
|
||||
// "strictFunctionTypes": true /* Enable strict checking of function types. */,
|
||||
// "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
|
||||
// "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
|
||||
// "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,
|
||||
|
||||
/* Additional Checks */
|
||||
// "noUnusedLocals": true /* Report errors on unused locals. */,
|
||||
// "noUnusedParameters": true /* Report errors on unused parameters. */,
|
||||
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
|
||||
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
|
||||
|
||||
/* Debugging Options */
|
||||
"traceResolution": false /* Report module resolution log messages. */,
|
||||
"listEmittedFiles": false /* Print names of generated files part of the compilation. */,
|
||||
"listFiles": false /* Print names of files part of the compilation. */,
|
||||
"pretty": true /* Stylize errors and messages using color and context. */,
|
||||
|
||||
/* Experimental Options */
|
||||
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
|
||||
"emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
|
||||
|
||||
"target": "es5",
|
||||
"typeRoots": [
|
||||
"node_modules/@types"
|
||||
],
|
||||
"types": [
|
||||
],
|
||||
"lib": [
|
||||
"es2015", "es2016", "es2017", "dom"
|
||||
]
|
||||
}
|
||||
}
|
34
tslint.json
Normal file
34
tslint.json
Normal file
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"extends": ["tslint:latest", "tslint-config-prettier", "tslint-immutable"],
|
||||
"rules": {
|
||||
"interface-name": [true, "never-prefix"],
|
||||
// TODO: allow devDependencies only in **/*.spec.ts files:
|
||||
// waiting on https://github.com/palantir/tslint/pull/3708
|
||||
"no-implicit-dependencies": [true, "dev"],
|
||||
|
||||
/* tslint-immutable rules */
|
||||
// Recommended built-in rules
|
||||
"no-var-keyword": true,
|
||||
"no-parameter-reassignment": true,
|
||||
"typedef": [true, "call-signature"],
|
||||
|
||||
// Immutability rules
|
||||
"readonly-keyword": true,
|
||||
"readonly-array": true,
|
||||
"no-let": true,
|
||||
"no-object-mutation": true,
|
||||
"no-delete": true,
|
||||
"no-method-signature": true,
|
||||
|
||||
// Functional style rules
|
||||
"no-this": true,
|
||||
"no-class": true,
|
||||
"no-mixed-interface": true,
|
||||
"no-expression-statement": [
|
||||
true,
|
||||
{ "ignore-prefix": ["console.", "process.exit"] }
|
||||
],
|
||||
"no-if-statement": true
|
||||
/* end tslint-immutable rules */
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user