-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support logging file numbers * update version
- Loading branch information
Showing
7 changed files
with
128 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {NgxLoggerLevel} from './types/logger-lever.enum'; | ||
|
||
export class HttpMetaDataInterface { | ||
level: NgxLoggerLevel; | ||
timestamp: string; | ||
fileName: string; | ||
lineNumber: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import {NgxLoggerLevel} from '../types/logger-lever.enum'; | ||
|
||
export class NGXLoggerUtils { | ||
|
||
static prepareMetaString(timestamp: string, logLevel: string, fileName: string, lineNumber: string) { | ||
return `${timestamp} ${logLevel} [${fileName}:${lineNumber}]`; | ||
} | ||
|
||
static getColor(level: NgxLoggerLevel): 'blue' | 'teal' | 'gray' | 'red' | undefined { | ||
switch (level) { | ||
case NgxLoggerLevel.TRACE: | ||
return 'blue'; | ||
case NgxLoggerLevel.DEBUG: | ||
return 'teal'; | ||
case NgxLoggerLevel.INFO: | ||
case NgxLoggerLevel.LOG: | ||
return 'gray'; | ||
case NgxLoggerLevel.WARN: | ||
case NgxLoggerLevel.ERROR: | ||
return 'red'; | ||
case NgxLoggerLevel.OFF: | ||
default: | ||
return; | ||
} | ||
} | ||
|
||
|
||
|
||
/** | ||
* This allows us to see who called the logger | ||
* @return {string} | ||
* @private | ||
*/ | ||
static getCallerDetails(): {lineNumber: string, fileName: string} { | ||
const err = (new Error('')); | ||
|
||
// this should produce the line which NGX Logger was called | ||
const callerLine = err.stack.split('\n')[4].split('/'); | ||
|
||
// returns the file:lineNumber | ||
const fileLineNumber = callerLine[callerLine.length - 1].replace(/[)]/g, '').split(':'); | ||
|
||
return { | ||
fileName: fileLineNumber[0], | ||
lineNumber: fileLineNumber[1] | ||
} | ||
} | ||
|
||
static prepareMessage(message) { | ||
try { | ||
if (message instanceof Error) { | ||
message = message.stack; | ||
} else if (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().'; | ||
} | ||
|
||
return message; | ||
} | ||
|
||
static prepareAdditionalParameters(additional: any[]) { | ||
if (additional === null || additional === undefined) { | ||
return null; | ||
} | ||
|
||
return additional.map((next, idx) => { | ||
try { | ||
// We just want to make sure the JSON can be parsed, we do not want to actually change the type | ||
if (typeof next === 'object') { | ||
JSON.stringify(next) | ||
} | ||
|
||
return next; | ||
} | ||
catch (e) { | ||
return `The additional[${idx}] value could not be parsed using JSON.stringify().` | ||
} | ||
}); | ||
} | ||
|
||
|
||
} |