timer-logs
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -8,3 +8,3 @@ "use strict"; | ||
omitStackTrace: true, | ||
environment: "development", | ||
environment: "production", | ||
}); | ||
@@ -32,4 +32,12 @@ timer.customError("Hellp!"); | ||
}; | ||
const name = "bob"; | ||
timer.tsql `SELECT * FROM persons p join birthdays b ON p.date=b.date WHERE name='tom' AND b.age > 35 OR name=${name}`; | ||
const order = { code: "0001", qty: 81, items: [1, 2, 3] }; | ||
timer.tlog `New order received: ${order}`; | ||
postgresExample().then(() => { | ||
timer.info("Some information about the current state of the program:", false, 1, 2, 3, ["one", "two", "three"]); | ||
timer.info("Some info about the state of the program:", false, 1, 2, 3, [ | ||
"one", | ||
"two", | ||
"three", | ||
]); | ||
timer.alert("Something has gone seriously wrong!"); | ||
@@ -36,0 +44,0 @@ timer.warn("This shouldn't happen under normal circumstances, but isn't a catastrophe"); |
@@ -8,3 +8,3 @@ import Timer from "../index"; | ||
omitStackTrace: true, | ||
environment: "development", | ||
environment: "production", | ||
}); | ||
@@ -45,11 +45,15 @@ | ||
}; | ||
const name = "bob"; | ||
// log a SQL query with a parameter using the template tag | ||
timer.tsql`SELECT * FROM persons p join birthdays b ON p.date=b.date WHERE name='tom' AND b.age > 35 OR name=${name}`; | ||
const order = { code: "0001", qty: 81, items: [1, 2, 3] }; | ||
timer.tlog`New order received: ${order}`; | ||
postgresExample().then(() => { | ||
timer.info( | ||
"Some information about the current state of the program:", | ||
false, | ||
1, | ||
2, | ||
3, | ||
["one", "two", "three"] | ||
); | ||
timer.info("Some info about the state of the program:", false, 1, 2, 3, [ | ||
"one", | ||
"two", | ||
"three", | ||
]); | ||
timer.alert("Something has gone seriously wrong!"); | ||
@@ -56,0 +60,0 @@ timer.warn( |
@@ -94,2 +94,10 @@ import { PostgresError } from "./types/dataStructures/Errors"; | ||
/** | ||
* Log a tagged template literal. | ||
*/ | ||
tlog(strings: TemplateStringsArray, ...values: any[]): void; | ||
/** | ||
* A tagged template literal to print out a SQL query. | ||
*/ | ||
tsql(strings: TemplateStringsArray, ...values: any[]): void; | ||
/** | ||
* Logs a custom error message in a separate log to the main Timer | ||
@@ -96,0 +104,0 @@ * @param message the string to log |
13
index.js
@@ -9,2 +9,3 @@ "use strict"; | ||
const ProductionPresenter_1 = require("./src/presenters/ProductionPresenter"); | ||
const sqlPresenter_1 = require("./src/presenters/sqlPresenter"); | ||
class Timer { | ||
@@ -131,2 +132,14 @@ constructor(config) { | ||
} | ||
tlog(strings, ...values) { | ||
console.log(strings | ||
.flatMap((s, i) => [s, i < values.length && sqlPresenter_1.valueToString(values[i])].filter((s) => s)) | ||
.join("")); | ||
} | ||
tsql(strings, ...values) { | ||
const queryText = strings.reduce((query, phrase, index) => index < values.length | ||
? `${query} ${phrase}$${index + 1}` | ||
: `${query} ${phrase}`, ""); | ||
if (this.environment === Environment_1.Environment.DEVELOPMENT) | ||
console.log(sqlPresenter_1.PresentSql(queryText, values)); | ||
} | ||
customError(message) { | ||
@@ -133,0 +146,0 @@ const errorDetails = new Map(Object.entries({ message })); |
29
index.ts
@@ -10,2 +10,3 @@ import * as crypto from "crypto"; | ||
import { ProductionPresenter } from "./src/presenters/ProductionPresenter"; | ||
import { PresentSql, valueToString } from "./src/presenters/sqlPresenter"; | ||
@@ -243,2 +244,30 @@ export default class Timer { | ||
/** | ||
* Log a tagged template literal. | ||
*/ | ||
public tlog(strings: TemplateStringsArray, ...values: any[]) { | ||
console.log( | ||
strings | ||
.flatMap((s, i) => | ||
[s, i < values.length && valueToString(values[i])].filter((s) => s) | ||
) | ||
.join("") | ||
); | ||
} | ||
/** | ||
* A tagged template literal to print out a SQL query. | ||
*/ | ||
public tsql(strings: TemplateStringsArray, ...values: any[]) { | ||
const queryText = strings.reduce( | ||
(query, phrase, index) => | ||
index < values.length | ||
? `${query} ${phrase}$${index + 1}` | ||
: `${query} ${phrase}`, | ||
"" | ||
); | ||
if (this.environment === Environment.DEVELOPMENT) | ||
console.log(PresentSql(queryText, values)); | ||
} | ||
/** | ||
* Logs a custom error message in a separate log to the main Timer | ||
@@ -245,0 +274,0 @@ * @param message the string to log |
{ | ||
"name": "timer-logs", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "@types/node": "^15.00.0", |
@@ -10,1 +10,13 @@ import { ColourConverter } from "../../../types/interfaces/ColourConverter"; | ||
export declare const lightBlue: ColourConverter; | ||
export declare function wrap(message: string, colour: string): string; | ||
export declare function rgb(r: number, g: number, b: number): string; | ||
export declare const colourTable: { | ||
red: string; | ||
pink: string; | ||
orange: string; | ||
purple: string; | ||
blue: string; | ||
green: string; | ||
yellow: string; | ||
lightBlue: string; | ||
}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.lightBlue = exports.yellow = exports.green = exports.blue = exports.purple = exports.orange = exports.pink = exports.red = void 0; | ||
exports.colourTable = exports.rgb = exports.wrap = exports.lightBlue = exports.yellow = exports.green = exports.blue = exports.purple = exports.orange = exports.pink = exports.red = void 0; | ||
exports.red = (message) => { | ||
return wrap(message, colourTable["red"]); | ||
return wrap(message, exports.colourTable["red"]); | ||
}; | ||
exports.pink = (message) => { | ||
return wrap(message, colourTable["pink"]); | ||
return wrap(message, exports.colourTable["pink"]); | ||
}; | ||
exports.orange = (message) => { | ||
return wrap(message, colourTable["orange"]); | ||
return wrap(message, exports.colourTable["orange"]); | ||
}; | ||
exports.purple = (message) => { | ||
return wrap(message, colourTable["purple"]); | ||
return wrap(message, exports.colourTable["purple"]); | ||
}; | ||
exports.blue = (message) => { | ||
return wrap(message, colourTable["blue"]); | ||
return wrap(message, exports.colourTable["blue"]); | ||
}; | ||
exports.green = (message) => { | ||
return wrap(message, colourTable["green"]); | ||
return wrap(message, exports.colourTable["green"]); | ||
}; | ||
exports.yellow = (message) => { | ||
return wrap(message, colourTable["yellow"]); | ||
return wrap(message, exports.colourTable["yellow"]); | ||
}; | ||
exports.lightBlue = (message) => { | ||
return wrap(message, colourTable["lightBlue"]); | ||
return wrap(message, exports.colourTable["lightBlue"]); | ||
}; | ||
@@ -31,6 +31,8 @@ function wrap(message, colour) { | ||
} | ||
exports.wrap = wrap; | ||
function rgb(r, g, b) { | ||
return `\x1b[38;2;${r};${g};${b}m`; | ||
} | ||
const colourTable = { | ||
exports.rgb = rgb; | ||
exports.colourTable = { | ||
red: rgb(235, 77, 75), | ||
@@ -37,0 +39,0 @@ pink: rgb(255, 121, 121), |
@@ -28,11 +28,11 @@ import { ColourConverter } from "../../../types/interfaces/ColourConverter"; | ||
function wrap(message: string, colour: string) { | ||
export function wrap(message: string, colour: string) { | ||
return `${colour}${message}\x1b[0m`; | ||
} | ||
function rgb(r: number, g: number, b: number) { | ||
export function rgb(r: number, g: number, b: number) { | ||
return `\x1b[38;2;${r};${g};${b}m`; | ||
} | ||
const colourTable = { | ||
export const colourTable = { | ||
red: rgb(235, 77, 75), | ||
@@ -39,0 +39,0 @@ pink: rgb(255, 121, 121), |
@@ -6,6 +6,6 @@ "use strict"; | ||
const SeverityColours_1 = require("./colours/SeverityColours"); | ||
exports.DevelopmentPresenter = (log) => { | ||
exports.DevelopmentPresenter = async (log) => { | ||
var _a; | ||
const filenameWidth = 20; | ||
process.stdout.write([ | ||
const filenameWidth = 30; | ||
const output = [ | ||
ColourConverters_1.purple(new Date(log.timestamp).toLocaleTimeString()), | ||
@@ -20,4 +20,4 @@ ((_a = SeverityColours_1.SeverityColours.get(log.severity)) !== null && _a !== void 0 ? _a : ColourConverters_1.red)(`${log.severity.padEnd(7, " ")}`), | ||
log.message, | ||
].join(" | ") + "\n"); | ||
return Promise.resolve(); | ||
].join(" | ") + "\n"; | ||
await new Promise((resolve) => process.stdout.write(output, resolve)); | ||
}; |
@@ -10,5 +10,5 @@ import {LogPresenter} from "../../types/interfaces/LogPresenter"; | ||
*/ | ||
export const DevelopmentPresenter: LogPresenter = (log) => { | ||
const filenameWidth = 20; | ||
process.stdout.write( | ||
export const DevelopmentPresenter: LogPresenter = async (log) => { | ||
const filenameWidth = 30; | ||
const output: string = | ||
[ | ||
@@ -26,5 +26,4 @@ purple(new Date(log.timestamp).toLocaleTimeString()), | ||
log.message, | ||
].join(" | ") + "\n" | ||
); | ||
return Promise.resolve(); | ||
].join(" | ") + "\n"; | ||
await new Promise((resolve) => process.stdout.write(output, resolve)); | ||
}; |
@@ -5,4 +5,4 @@ "use strict"; | ||
exports.ProductionPresenter = async (log) => { | ||
const logString = JSON.stringify(log); | ||
const logString = JSON.stringify(log) + "\n"; | ||
await new Promise((resolve) => process.stdout.write(logString, resolve)); | ||
}; |
@@ -1,2 +0,2 @@ | ||
import {LogPresenter} from "../../types/interfaces/LogPresenter"; | ||
import { LogPresenter } from "../../types/interfaces/LogPresenter"; | ||
@@ -9,4 +9,4 @@ /** | ||
export const ProductionPresenter: LogPresenter = async (log) => { | ||
const logString: string = JSON.stringify(log); | ||
const logString: string = JSON.stringify(log) + "\n"; | ||
await new Promise((resolve) => process.stdout.write(logString, resolve)); | ||
}; |
Sorry, the diff of this file is not supported yet
95744
61
1965