Skip to content

Commit

Permalink
feat: logs now always render and printed boolean now returned when te…
Browse files Browse the repository at this point in the history
…rminated (#93)

Logs will always render and store their render. To know if a log was printed to the console
terminated logs now expose the "printed" boolean that indicates if it was printed. Log listeners
also now receive the printed boolean. Rather than checking for render equal to null, listeners
should now check the printed boolean.

Co-authored-by: Andrew Stacy <[email protected]>
  • Loading branch information
Andrew Stacy and AJStacy authored Dec 2, 2021
1 parent 45816e2 commit f5e9413
Show file tree
Hide file tree
Showing 33 changed files with 186 additions and 166 deletions.
2 changes: 2 additions & 0 deletions src/_contracts/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export interface LogData {
dumpContext: boolean;
expression?: boolean;
isSilent: boolean;
printed: boolean;
label: LabelData;
level: number | null;
meta: MetaData;
Expand Down Expand Up @@ -104,4 +105,5 @@ export interface FinalLogData extends LogData {
export interface TerminatedLog<I extends BaseLog> {
log: I;
render: LogRender | null;
printed: boolean;
}
3 changes: 2 additions & 1 deletion src/_contracts/Shed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export type ListenerBucket = Map<number, ListenerCallback>;

export type ListenerCallback = (
LogData: LogData | FinalLogData,
render: LogRender | null
render: LogRender | null,
printed: boolean
) => void;

export interface ShedConfig {
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export { adze } from './adze';
import { adze } from './adze';
export { adze };
export default adze;
export { Label } from './label';
export { defaults } from './_defaults';
export {
Expand Down
69 changes: 40 additions & 29 deletions src/log/BaseLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ export class BaseLog {
*/
private isSilent = false;

/**
* Flag which indicates if the log is allowed to print to the console.
*/
private printed = false;

/**
* Flag which tells the log instance to add the
* MDC context to the log render arguments.
Expand Down Expand Up @@ -765,41 +770,41 @@ export class BaseLog {
// Apply modifiers in the proper order.
this.runModifierQueue();

// Check the test modifiers.
if (this.evalPasses()) {
// Save values to this log instance for later recall
this.args = args;
this._level = def.level;
this.definition = def;
this._timestamp = timestamp();
this.stacktrace = this.cfg.captureStacktrace ? stacktrace() : null;

// Set this log data to a variable for type checking
const log_data = this.data;

if (isFinalLogData(log_data)) {
// Render the log if it's allowed to print
this._render = allowed(log_data)
? new Printer(log_data)[this.printer]()
: null;

// Attempt to print the render to the console / terminal
toConsole(this._render);
// Save values to this log instance for later recall
this.args = args;
this._level = def.level;
this.definition = def;
this._timestamp = timestamp();
this.stacktrace = this.cfg.captureStacktrace ? stacktrace() : null;

// Set this log data to a variable for type checking
const log_data = this.data;

// Cache the log
this.store();
if (isFinalLogData(log_data)) {
// Render the log
this._render = new Printer(log_data)[this.printer]();

// Fire the log listeners
this.fireListeners(log_data, this._render);
// Evaluates if this log can print
this.printed = allowed(log_data) && this.evalPasses();

// Return the terminated log object with a render
return { log: this, render: this._render };
// Attempt to print the render to the console / terminal
if (this.printed) {
toConsole(this._render);
}

// Cache the log
this.store();

// Fire the log listeners
this.fireListeners(log_data, this._render, this.printed);

// Return the terminated log object with a render
return { log: this, render: this._render, printed: this.printed };
}
}

// Return the terminated log object unrendered
return { log: this, render: null };
return { log: this, render: null, printed: false };
}

/**
Expand Down Expand Up @@ -838,10 +843,14 @@ export class BaseLog {
/**
* Fires listeners for this log instance if a Shed exists.
*/
private fireListeners(data: FinalLogData, render: LogRender | null): void {
private fireListeners(
data: FinalLogData,
render: LogRender | null,
printed: boolean
): void {
const shed = this.env.global.$shed;
if (shedExists(shed)) {
shed.fireListeners(data, render);
shed.fireListeners(data, render, printed);
}
}

Expand Down Expand Up @@ -871,6 +880,7 @@ export class BaseLog {
expression: this.expression,
dumpContext: this.dumpContext,
isSilent: this.isSilent,
printed: this.printed,
showTimestamp: this.showTimestamp,
timeNow: this.timeNowVal,
meta: { ...this.metaData },
Expand All @@ -896,6 +906,7 @@ export class BaseLog {
this.expression = data.expression;
this.dumpContext = data.dumpContext;
this.isSilent = data.isSilent;
this.printed = data.printed;
this.showTimestamp = data.showTimestamp;
this.timeNowVal = data.timeNow;
this.metaData = { ...data.meta };
Expand Down
8 changes: 6 additions & 2 deletions src/shed/Shed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,13 @@ export class Shed {
* Fires any listeners that are watching the log level defined in the provided log data. The log data
* and render object will be passed to the listener callback.
*/
public fireListeners(log: FinalLogData, render: LogRender | null): void {
public fireListeners(
log: FinalLogData,
render: LogRender | null,
printed: boolean
): void {
this.listeners.get(log.level)?.forEach((listener) => {
listener(log, render);
listener(log, render, printed);
});
}

Expand Down
Loading

0 comments on commit f5e9413

Please sign in to comment.