Skip to content

Commit

Permalink
Merge pull request #39 from drashland/feat/improve-api
Browse files Browse the repository at this point in the history
feat: allow params in log messages
  • Loading branch information
crookse authored Oct 19, 2022
2 parents 86a3057 + 303b64c commit c0cdfeb
Show file tree
Hide file tree
Showing 6 changed files with 682 additions and 145 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@ off: does not log any messages
```

So this means that a `logger.warn("hello")` will not log if `level` is `error`.

## Log message params

Log messages can take in params by specifying `{}` in the log message. For
example:

```typescript
logger.debug("Hello {}", "world") => Outputs "[DEBUG] Hello world"
logger.debug("Hello {} {}", "world", "world") => Outputs "[DEBUG] Hello world world"
logger.debug("Hello {} {}", "world") => Outputs "[DEBUG] Hello world {}"
logger.debug("Hello {} {}", "world", false) => Outputs "[DEBUG] Hello world false"
logger.debug("Hello {} {}", "world", true) => Outputs "[DEBUG] Hello world true"
logger.debug("Hello {} {}", "world", SomeClass) => Outputs "[DEBUG] Hello world SomeClass"
logger.debug("Hello {} {}", "world", funcDec) => Outputs "[DEBUG] Hello world funcDec"
logger.debug("Hello {} {}", "world", funcExp) => Outputs "[DEBUG] Hello world funcExp"
logger.debug("Hello {}", "world", funcExp, "nope") => Outputs "[DEBUG] Hello world"
```
36 changes: 22 additions & 14 deletions src/console_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export class ConsoleLogger extends Logger {
*
* @returns The full message that will be logged
*/
public debug(message: string): string | void {
return this.#logToConsole(message, "debug");
public debug(message: string, ...params: unknown[]): string | void {
return this.#logToConsole(message, "debug", params);
}

/**
Expand All @@ -23,8 +23,8 @@ export class ConsoleLogger extends Logger {
*
* @returns The full message that will be logged
*/
public error(message: string): string | void {
return this.#logToConsole(message, "error");
public error(message: string, ...params: unknown[]): string | void {
return this.#logToConsole(message, "error", params);
}

/**
Expand All @@ -34,8 +34,8 @@ export class ConsoleLogger extends Logger {
*
* @returns The full message that will be logged
*/
public info(message: string): string | void {
return this.#logToConsole(message, "info");
public info(message: string, ...params: unknown[]): string | void {
return this.#logToConsole(message, "info", params);
}

/**
Expand All @@ -45,8 +45,8 @@ export class ConsoleLogger extends Logger {
*
* @returns The full message that will be logged
*/
public warn(message: string): string | void {
return this.#logToConsole(message, "warn");
public warn(message: string, ...params: unknown[]): string | void {
return this.#logToConsole(message, "warn", params);
}

/**
Expand All @@ -56,8 +56,8 @@ export class ConsoleLogger extends Logger {
*
* @returns The full message that will be logged
*/
public fatal(message: string): string | void {
return this.#logToConsole(message, "fatal");
public fatal(message: string, ...params: unknown[]): string | void {
return this.#logToConsole(message, "fatal", params);
}

/**
Expand All @@ -67,8 +67,8 @@ export class ConsoleLogger extends Logger {
*
* @returns The full message that will be logged
*/
public trace(message: string): string | void {
return this.#logToConsole(message, "trace");
public trace(message: string, ...params: unknown[]): string | void {
return this.#logToConsole(message, "trace", params);
}

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -84,14 +84,22 @@ export class ConsoleLogger extends Logger {
*
* @returns The end message that will be logged
*/
#logToConsole(message: string, logType: LogTypes): string | void {
#logToConsole(
message: string,
logType: LogTypes,
params: unknown[],
): string | void {
if (!this.shouldLog(logType)) {
return;
}

this.current_log_message_level_name = logType;

const fullLogMessage = this.constructFullLogMessage(message, logType);
const fullLogMessage = this.constructFullLogMessage(
message,
logType,
params,
);
console.log(fullLogMessage);
return fullLogMessage;
}
Expand Down
27 changes: 14 additions & 13 deletions src/file_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class FileLogger extends Logger {
*
* @returns The full message that will be logged
*/
public debug(message: string): string | void {
return this.#logToFile(message, "debug", this.filename);
public debug(message: string, ...params: unknown[]): string | void {
return this.#logToFile(message, "debug", this.filename, params);
}

/**
Expand All @@ -43,8 +43,8 @@ export class FileLogger extends Logger {
*
* @returns The full message that will be logged
*/
public error(message: string): string | void {
return this.#logToFile(message, "error", this.filename);
public error(message: string, ...params: unknown[]): string | void {
return this.#logToFile(message, "error", this.filename, params);
}

/**
Expand All @@ -54,8 +54,8 @@ export class FileLogger extends Logger {
*
* @returns The full message that will be logged
*/
public info(message: string): string | void {
return this.#logToFile(message, "info", this.filename);
public info(message: string, ...params: unknown[]): string | void {
return this.#logToFile(message, "info", this.filename, params);
}

/**
Expand All @@ -65,8 +65,8 @@ export class FileLogger extends Logger {
*
* @returns The full message that will be logged
*/
public warn(message: string): string | void {
return this.#logToFile(message, "warn", this.filename);
public warn(message: string, ...params: unknown[]): string | void {
return this.#logToFile(message, "warn", this.filename, params);
}

/**
Expand All @@ -76,8 +76,8 @@ export class FileLogger extends Logger {
*
* @returns The full message that will be logged
*/
public fatal(message: string): string | void {
return this.#logToFile(message, "fatal", this.filename);
public fatal(message: string, ...params: unknown[]): string | void {
return this.#logToFile(message, "fatal", this.filename, params);
}

/**
Expand All @@ -87,8 +87,8 @@ export class FileLogger extends Logger {
*
* @returns The full message that will be logged
*/
public trace(message: string): string | void {
return this.#logToFile(message, "trace", this.filename);
public trace(message: string, ...params: unknown[]): string | void {
return this.#logToFile(message, "trace", this.filename, params);
}

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -109,14 +109,15 @@ export class FileLogger extends Logger {
message: string,
logType: LogTypes,
filename: string,
params: unknown[],
): string | void {
if (!this.shouldLog(logType)) {
return;
}

this.current_log_message_level_name = logType;

const line = this.constructFullLogMessage(message, logType);
const line = this.constructFullLogMessage(message, logType, params);
Deno.writeFileSync(filename, encoder.encode(line + "\n"), { append: true });
return line;
}
Expand Down
80 changes: 76 additions & 4 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export abstract class Logger {
*
* @param configs - Config used for Logging
*/
constructor(configs: LoggerConfigs) {
constructor(configs?: LoggerConfigs) {
if (!configs) {
configs = {};
}

if (!configs.level) {
configs.level = "debug";
}
Expand All @@ -72,7 +76,10 @@ export abstract class Logger {
*
* @returns Return the full logged message.
*/
abstract debug(message: string): string | void;
abstract debug(
message: string | unknown,
...params: unknown[]
): string | void;

/**
* Write a message to the console. Prefixed with the log type
Expand Down Expand Up @@ -135,6 +142,7 @@ export abstract class Logger {
protected constructFullLogMessage(
message: string,
logType: LogTypes,
params: unknown[],
): string {
const messageToColor = `[${logType.toUpperCase()}]`;
let prefix = "";
Expand Down Expand Up @@ -163,7 +171,11 @@ export abstract class Logger {
if (tagString) {
message = tagString + " " + message;
}

message = prefix + " " + message;

message = this.fillParams(message, params);

return message;
}

Expand Down Expand Up @@ -192,15 +204,75 @@ export abstract class Logger {
}

//////////////////////////////////////////////////////////////////////////////
// FILE MARKER - METHODS - PRIVATE ///////////////////////////////////////////
// FILE MARKER - METHODS - PROTECTED /////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

/**
* Fill the parameters in the message.
* @param message The message to fill.
* @param params The params to use to fill the message.
* @return The message with all parameters used. If number of placeholders is
* greater than the number of params, then those placeholders will be returned
* as "{}" in the message.
*/
protected fillParams(message: string, params: unknown[]): string {
if (params && params.length > 0) {
try {
const parts = message.split("{}");

return parts.map((part: string, index: number) => {
// If there are no more parts, then there is no reason to continue
// adding params to the message
if ((index + 1) >= parts.length) {
return part;
}

const param = params[index];

// If the param is a function, then log its name. We do not intend
// to output its implementation.
if (typeof param === "function") {
part += param.name;
return part;
}

// If the param is an object, then we can JSON stringify it to show
// a proper "string object" form of it. If a different output is
// required, then the extended class can do so.
if (typeof param === "object") {
part += JSON.stringify(param);
return part;
}

if (param === undefined) {
// If the param is undefined because it was not given, then we use
// the placeholder
if (index >= params.length) {
return part += "{}";
}

// Otherwise, it was given as `undefined`, so we output exactly that
return part += "undefined";
}

part += params[index] ?? "{}";
return part;
})
.join("");
} catch (_error) {
// Not sure what to do here...
}
}

return message;
}

/**
* Get the parsed version of the raw tag string.
*
* @return The tag string
*/
private getTagStringParsed(): string {
protected getTagStringParsed(): string {
if (this.configs.tag_string && this.configs.tag_string.trim() == "") {
return "";
}
Expand Down
Loading

0 comments on commit c0cdfeb

Please sign in to comment.