...
 
Commits (2)
......@@ -24,7 +24,7 @@ Error("error message");
## Web usage (global script import)
Include: `<script src="https://unpkg.com/[email protected]4/dist/index.webpack.js"></script>`
Include: `<script src="https://unpkg.com/[email protected]5/dist/index.webpack.js"></script>`
```javascript
// functions are bound to window object
......
......@@ -18,7 +18,7 @@
"prod:node": "tsc"
},
"name": "log74",
"version": "1.0.4",
"version": "1.0.5",
"description": "Mini logging library for Node.js and the web.",
"main": "dist/index.node.js",
"repository": "https://gitlab.insrt.uk/insert/log74.git",
......
import { Result } from './result';
import { Result, LogLevel, LevelString, ShouldNotLog } from './result';
export type Handler = (level: LevelString, string: string, cb?: Result) => void;
/**
* Logger class with custom message handler
*/
export default class Logger {
l: Result;
level: LogLevel;
handler: Handler;
out?: Result;
constructor(handler: Handler, level?: LogLevel, out?: Result) {
this.handler = handler;
this.out = out;
this.level = level || LogLevel.Log;
}
error(string: string) {
if (ShouldNotLog(LogLevel.Error, this.level)) return;
this.handler('error', string, this.out);
}
warn(string: string) {
if (ShouldNotLog(LogLevel.Warn, this.level)) return;
this.handler('warn', string, this.out);
}
info(string: string) {
if (ShouldNotLog(LogLevel.Info, this.level)) return;
this.handler('info', string, this.out);
}
success(string: string) {
if (ShouldNotLog(LogLevel.Success, this.level)) return;
this.handler('success', string, this.out);
}
log(string: string) {
if (ShouldNotLog(LogLevel.Log, this.level)) return;
this.handler('log', string, this.out);
}
constructor(log: Result) {
this.l = log;
debug(string: string) {
if (ShouldNotLog(LogLevel.Debug, this.level)) return;
this.handler('debug', string, this.out);
}
};
\ No newline at end of file
......@@ -7,4 +7,22 @@ export enum LogLevel {
Debug
};
export type Result = (raw: string, level: LogLevel, formatted?: string) => void;
\ No newline at end of file
export type LevelString = 'error' | 'warn' | 'info' | 'success' | 'log' | 'debug';
export function GetLevel(level: LevelString) {
switch (level) {
case 'error': return LogLevel.Error;
case 'warn': return LogLevel.Warn;
case 'info': return LogLevel.Info;
case 'success': return LogLevel.Success;
case 'log': return LogLevel.Log;
case 'debug': return LogLevel.Debug;
default: return LogLevel.Log;
}
}
export function ShouldNotLog(level: LogLevel, allowed: LogLevel) {
if (level > allowed) return true;
return false;
}
export type Result = (raw: string, formatted?: string) => void;
\ No newline at end of file
import chalk from 'chalk';
import { Result, LogLevel } from './common/result';
import { Result, LogLevel, LevelString } from './common/result';
import LoggerClass from './common/logger';
/**
* Write an error message
* Write a message
*/
export function Error(string: string, cb?: Result) {
let output = chalk`{bgRed {white ✖ ERR }} {red ${string}}`;
console.error(output);
cb && cb(`[${new Date().toUTCString()}] ERR: ${string}`, LogLevel.Error, output);
export function Log74(level: LevelString, string: string, cb?: Result) {
let output;
switch (level) {
case 'error':
output = chalk`{bgRed {white ✖ ERR }} {red ${string}}`;
break;
case 'warn':
output = chalk`{bgYellow {black ⚠ WARN }} {white ${string}}`;
break;
case 'info':
output = chalk`{bgBlue {white ℹ️ INFO }} {white ${string}}`;
break;
case 'success':
output = chalk`{bgGreen {black ✔️ SUCCESS }} {white ${string}}`;
break;
case 'log':
output = chalk`{bgWhite {black LOG }} {gray ${string}}`;
break;
case 'debug':
output = chalk`{gray [DEBUG] ${string}}`;
break;
default:
output = chalk`{gray [?]} ${string}`;
}
let target = level === 'error' ? console.error :
level === 'warn' ? console.warn :
level === 'debug' ? console.debug :
console.log;
target(output);
if (cb) {
cb(`[${new Date().toUTCString()}] ${level.toUpperCase()}: ${string}`, output);
}
}
/**
* Write an error message
*/
export function Error(string: string, cb?: Result) { Log74('warn', string, cb); }
/**
* Write a warning message
*/
export function Warn(string: string, cb?: Result) {
let output = chalk`{bgYellow {black ⚠ WARN }} {white ${string}}`;
console.warn(output);
cb && cb(`[${new Date().toUTCString()}] WARN: ${string}`, LogLevel.Warn, output);
}
export function Warn(string: string, cb?: Result) { Log74('warn', string, cb); }
/**
* Write an info message
*/
export function Info(string: string, cb?: Result) {
let output = chalk`{bgBlue {white ℹ️ INFO }} {white ${string}}`;
console.info(output);
cb && cb(`[${new Date().toUTCString()}] INFO: ${string}`, LogLevel.Info, output);
}
export function Info(string: string, cb?: Result) { Log74('info', string, cb); }
/**
* Write a success message
*/
export function Success(string: string, cb?: Result) {
let output = chalk`{bgGreen {black ✔️ SUCCESS }} {white ${string}}`;
console.log(output);
cb && cb(`[${new Date().toUTCString()}] SUCCESS: ${string}`, LogLevel.Success, output);
}
export function Success(string: string, cb?: Result) { Log74('success', string, cb); }
/**
* Write a 'notice' message
*/
export function Log(string: string, cb?: Result) {
let output = chalk`{bgWhite {black LOG }} {gray ${string}}`;
console.log(output);
cb && cb(`[${new Date().toUTCString()}] LOG: ${string}`, LogLevel.Log, output);
}
export function Log(string: string, cb?: Result) { Log74('log', string, cb); }
/**
* Write a debug message
*/
export function Debug(string: string, cb?: Result) {
let output = chalk`{gray [DEBUG] ${string}}`;
console.log(output);
cb && cb(`[${new Date().toUTCString()}] DEBUG: ${string}`, LogLevel.Debug, output);
}
export function Debug(string: string, cb?: Result) { Log74('debug', string, cb); }
/**
* Logger class with custom message handler
*/
export class Logger extends LoggerClass {
error(string: string) {Error(string, this.l)};
warn(string: string) {Warn(string, this.l)};
info(string: string) {Info(string, this.l)};
success(string: string) {Success(string, this.l)};
log(string: string) {Log(string, this.l)};
debug(string: string) {Debug(string, this.l)};
export default class Logger extends LoggerClass {
constructor(level?: LogLevel, cb?: Result) {
super(Log74, level, cb);
}
};
export default Logger;
export * from './common/result';
\ No newline at end of file
import { Result, LogLevel } from './common/result';
import Logger from './common/logger';
import { Result, LogLevel, LevelString } from './common/result';
import LoggerClass from './common/logger';
/**
* Write an error message
* Write a message
*/
export function Error(string: string, cb?: Result) {
console.error(`%c✖ ERR %c${string}`, 'color: red; font-size: 1.6em', 'color: white; font-size: 1.4em');
cb && cb(`[${new Date().toUTCString()}] ERR: ${string}`, LogLevel.Error);
export function Log74(level: LevelString, string: string, cb?: Result) {
let output;
switch (level) {
case 'error':
output = [`%c✖ ERR %c${string}`, 'color: red; font-size: 1.6em', 'color: white; font-size: 1.4em'];
break;
case 'warn':
output = [`%c⚠ WARN %c${string}`, 'color: yellow; font-size: 1.6em', 'color: white; font-size: 1.4em'];
break;
case 'info':
output = [`%cℹ️ INFO %c${string}`, 'color: #6298ef; font-size: 1.6em', 'color: white; font-size: 1.4em'];
break;
case 'success':
output = [`%c✔️ SUCCESS %c${string}`, 'color: green; font-size: 1.6em', 'color: white; font-size: 1.4em'];
break;
case 'log':
output = [`%cLOG %c${string}`, 'color: gray; font-size: 1.6em', 'color: white; font-size: 1.4em'];
break;
case 'debug':
output = [`%cDEBUG ${string}`, 'color: gray; font-size: 1.6em', 'font-size: 1.4em'];
break;
default:
output = [string];
}
let target = level === 'error' ? console.error :
level === 'warn' ? console.warn :
level === 'info' ? console.info :
level === 'debug' ? console.debug :
console.log;
target(...output);
if (cb) {
cb(`[${new Date().toUTCString()}] ${level.toUpperCase()}: ${string}`, output[0]);
}
}
/**
* Write an error message
*/
export function Error(string: string, cb?: Result) { Log74('warn', string, cb); }
/**
* Write a warning message
*/
export function Warn(string: string, cb?: Result) {
console.warn(`%c⚠ WARN %c${string}`, 'color: yellow; font-size: 1.6em', 'color: white; font-size: 1.4em');
cb && cb(`[${new Date().toUTCString()}] WARN: ${string}`, LogLevel.Warn);
}
export function Warn(string: string, cb?: Result) { Log74('warn', string, cb); }
/**
* Write an info message
*/
export function Info(string: string, cb?: Result) {
console.info(`%cℹ️ INFO %c${string}`, 'color: #6298ef; font-size: 1.6em', 'color: white; font-size: 1.4em');
cb && cb(`[${new Date().toUTCString()}] WARN: ${string}`, LogLevel.Info);
}
export function Info(string: string, cb?: Result) { Log74('info', string, cb); }
/**
* Write a success message
*/
export function Success(string: string, cb?: Result) {
console.info(`%c✔️ SUCCESS %c${string}`, 'color: green; font-size: 1.6em', 'color: white; font-size: 1.4em');
cb && cb(`[${new Date().toUTCString()}] SUCCESS: ${string}`, LogLevel.Success);
}
export function Success(string: string, cb?: Result) { Log74('success', string, cb); }
/**
* Write a 'notice' message
*/
export function Log(string: string, cb?: Result) {
console.info(`%cLOG %c${string}`, 'color: gray; font-size: 1.6em', 'color: white; font-size: 1.4em');
cb && cb(`[${new Date().toUTCString()}] LOG: ${string}`, LogLevel.Log);
}
export function Log(string: string, cb?: Result) { Log74('log', string, cb); }
/**
* Write a debug message
*/
export function Debug(string: string, cb?: Result) {
console.info(`%cDEBUG ${string}`, 'color: gray; font-size: 1.6em', 'font-size: 1.4em');
cb && cb(`[${new Date().toUTCString()}] LOG: ${string}`, LogLevel.Debug);
}
export function Debug(string: string, cb?: Result) { Log74('debug', string, cb); }
/**
* Logger class with custom message handler
*/
export class WebLogger extends Logger {
error(string: string) {Error(string, this.l)};
warn(string: string) {Warn(string, this.l)};
info(string: string) {Info(string, this.l)};
success(string: string) {Success(string, this.l)};
log(string: string) {Log(string, this.l)};
debug(string: string) {Debug(string, this.l)};
};
\ No newline at end of file
export default class Logger extends LoggerClass {
constructor(level?: LogLevel, cb?: Result) {
super(Log74, level, cb);
}
};
export * from './common/result';
\ No newline at end of file
......@@ -8,4 +8,4 @@ win.Info = web.Info;
win.Success = web.Success;
win.Log = web.Log;
win.Debug = web.Debug;
win.Logger = web.WebLogger;
\ No newline at end of file
win.Logger = web.default;
\ No newline at end of file