1
0
mirror of https://git.stamm.me/OpenServer/NodeLogging.git synced 2024-11-22 13:29:23 +00:00

Adding line support and adding default export

This commit is contained in:
Fabian Stamm 2018-05-12 18:46:47 +02:00
parent 932eeacb86
commit db9a6472c9
6 changed files with 1544 additions and 23 deletions

1
out/index.d.ts vendored
View File

@ -22,6 +22,7 @@ export declare class Logging {
private static checkQueue(); private static checkQueue();
private static initializeFile(); private static initializeFile();
} }
export default Logging;
export declare enum LoggingTypes { export declare enum LoggingTypes {
Log = 0, Log = 0,
Warning = 1, Warning = 1,

View File

@ -52,10 +52,6 @@ class Logging {
Logging.errorMessage(error + e.stack); Logging.errorMessage(error + e.stack);
return; return;
} }
// let m = "";
// (<any>error.stack).forEach(e => {
// m += e.toString() + "\n";
// })
var message = error.name + " " + error.message + "\n" + error.stack; var message = error.name + " " + error.message + "\n" + error.stack;
Logging.message(LoggingTypes.Error, [message]); Logging.message(LoggingTypes.Error, [message]);
} }
@ -99,7 +95,9 @@ class Logging {
} }
}); });
} }
var m = "[" + LoggingTypes[type] + "][" + _getCallerFile() + "][" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + "]: " + mb; let file = getCallerFile();
let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
var m = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: ${mb}`;
if (this.stdout) if (this.stdout)
console.log(consoleLogFormat + m + Reset); console.log(consoleLogFormat + m + Reset);
let index = m.indexOf("\x1b"); let index = m.indexOf("\x1b");
@ -224,6 +222,7 @@ Logging.writing = false;
Logging.queue = new Array(); Logging.queue = new Array();
Logging.events = new events_1.EventEmitter(); Logging.events = new events_1.EventEmitter();
exports.Logging = Logging; exports.Logging = Logging;
exports.default = Logging;
function fsUnlink(path) { function fsUnlink(path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.unlink(path, (err) => { fs.unlink(path, (err) => {
@ -303,18 +302,22 @@ function getStack() {
stack.shift(); // getStack --> Error stack.shift(); // getStack --> Error
return stack; return stack;
} }
function _getCallerFile() { function getCallerFile() {
try { try {
let stack = getStack(); let stack = getStack();
let current_file = stack.shift().getFileName(); let current_file = stack.shift().getFileName();
while (stack.length) { while (stack.length) {
let caller_file = stack.shift().getFileName(); let caller_file = stack.shift();
if (current_file !== caller_file) const util = require("util");
return path.basename(caller_file); if (current_file !== caller_file.getFileName())
return {
file: path.basename(caller_file.getFileName()),
line: caller_file.getLineNumber()
};
} }
} }
catch (err) { } catch (err) { }
return undefined; return { file: undefined, line: 0 };
} }
var LoggingTypes; var LoggingTypes;
(function (LoggingTypes) { (function (LoggingTypes) {

File diff suppressed because one or more lines are too long

View File

@ -1,12 +1,14 @@
{ {
"name": "logger-perfcloud", "name": "logger-perfcloud",
"version": "1.2.0", "version": "1.2.1",
"description": "", "description": "",
"main": "out/index.js", "main": "out/index.js",
"types": "out/index.d.ts", "types": "out/index.d.ts",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"test": "node out/test.js" "watch": "tsc --watch",
"test": "node out/test.js",
"live": "nodemon out/test.js"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@ -16,6 +18,7 @@
"license": "MIT", "license": "MIT",
"devDependencies": { "devDependencies": {
"@types/node": "^8.0.24", "@types/node": "^8.0.24",
"nodemon": "^1.17.4",
"typescript": "^2.4.2" "typescript": "^2.4.2"
} }
} }

View File

@ -74,10 +74,7 @@ export class Logging {
Logging.errorMessage(error + e.stack); Logging.errorMessage(error + e.stack);
return; return;
} }
// let m = "";
// (<any>error.stack).forEach(e => {
// m += e.toString() + "\n";
// })
var message = error.name + " " + error.message + "\n" + error.stack; var message = error.name + " " + error.message + "\n" + error.stack;
Logging.message(LoggingTypes.Error, [message]); Logging.message(LoggingTypes.Error, [message]);
} }
@ -119,8 +116,9 @@ export class Logging {
} }
}); });
} }
let file = getCallerFile()
var m = "[" + LoggingTypes[type] + "][" + _getCallerFile() + "][" + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '') + "]: " + mb; let date = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
var m = `[${LoggingTypes[type]}][${file.file}:${file.line}][${date}]: ${mb}`;
if (this.stdout) console.log(consoleLogFormat + m + Reset); if (this.stdout) console.log(consoleLogFormat + m + Reset);
let index = m.indexOf("\x1b"); let index = m.indexOf("\x1b");
@ -235,6 +233,8 @@ export class Logging {
} }
} }
export default Logging;
function fsUnlink(path) { function fsUnlink(path) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
fs.unlink(path, (err) => { fs.unlink(path, (err) => {
@ -325,18 +325,23 @@ function getStack() {
return stack return stack
} }
function _getCallerFile() { function getCallerFile() {
try { try {
let stack = getStack() let stack = getStack()
let current_file = stack.shift().getFileName(); let current_file = stack.shift().getFileName();
while (stack.length) { while (stack.length) {
let caller_file = stack.shift().getFileName(); let caller_file = stack.shift();
if (current_file !== caller_file) return path.basename(caller_file); const util = require("util")
if (current_file !== caller_file.getFileName())
return {
file: path.basename(caller_file.getFileName()),
line: caller_file.getLineNumber()
};
} }
} catch (err) { } } catch (err) { }
return undefined; return { file: undefined, line: 0 };
} }
export enum LoggingTypes { export enum LoggingTypes {

1509
yarn.lock

File diff suppressed because it is too large Load Diff