Skip to content

Commit

Permalink
feat(logger): adds hostname to the log output
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Jul 20, 2020
1 parent 7dd23cd commit a1f2b8d
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 274 deletions.
10 changes: 5 additions & 5 deletions packages/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ There is also the `printError` method on the `Ogma` class thta takes care of pri
If for tracing purposes you'd like to add a context to the log, or an application name, you can pass the context to the method related to the logLevel (such as `ogma.debug('debug message, SomeClass.name, 'NestJS')` and Ogma will print

```sh
[2019-12-19T23:01:23.900Z] [NestJS] 34760 [SomeClass] [Debug]| debug message
[2019-12-19T23:01:23.900Z] [hostname] [NestJS] 34760 [SomeClass] [Debug]| debug message
```

> Note: If colors are enabled, application will print in Yellow and context will print in Cyan.
> Note: If colors are enabled, hostname will print in magenta application will print in Yellow and context will print in Cyan.
When application and context are both present, Ogma will print your logs in a form as follows

```sh
[ISOString Date] [Application] PID [Context] [LogLevel]| message
[ISOString Date] [hostname] [Application] PID [Context] [LogLevel]| message
```

Examples can be seen below. The JSON structure follows the same form with log level and message being the last two properties.
Expand Down Expand Up @@ -107,10 +107,10 @@ I said the logs were beautiful, and to me they absolutely are. Each log is match

```shell
# ogma.log('hello')
[2019-12-11T22:54:58.462Z] 34760 [INFO] | hello
[2019-12-11T22:54:58.462Z] [hostname] 34760 [INFO] | hello

# ogma.log({a: () => 'hello', b: {c: 'nested'}, d: this});
[2019-12-11T22:56:02.502Z] 34760 [INFO] |
[2019-12-11T22:56:02.502Z] [hostname] 34760 [INFO] |
{
"a": "[Function]",
"b": {
Expand Down
Binary file modified packages/logger/ogma_sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/logger/ogma_sample_json.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion packages/logger/src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ function getContext(context: string, useColor: boolean): string {
return context;
}

function getHostname(hostname: string, useColor: boolean): string {
hostname = wrapInParens(hostname);
if (useColor) {
hostname = colorizeCLI(hostname, Color.MAGENTA);
}
return hostname;
}

async function rehydrate(
fileName: string,
useColor: boolean = process.stdout.isTTY,
Expand Down Expand Up @@ -99,14 +107,15 @@ async function rehydrate(
logs
.map((log) => JSON.parse(log))
.forEach((log: OgmaLog) => {
const { time, application, context, pid, level, ...rest } = log;
const { time, hostname, application, context, pid, level, ...rest } = log;
let message: string | Record<string, unknown>;
if (rest.message) {
message = getMessage(log);
} else {
message = getMessageFromJSON(rest);
}
let logMessage = wrapInParens(time) + ' ';
logMessage += getHostname(hostname, useColor) + ' ';
if (application) {
logMessage += getApplication(application, useColor) + ' ';
}
Expand Down
1 change: 1 addition & 0 deletions packages/logger/src/interfaces/ogma-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { LogLevel } from '../enums';

export interface OgmaLog {
time: string;
hostname: string;
application?: string;
context?: string;
message?: string;
Expand Down
15 changes: 14 additions & 1 deletion packages/logger/src/logger/ogma.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { hostname } from 'os';
import { Color, LogLevel } from '../enums';
import { OgmaDefaults, OgmaOptions } from '../interfaces';
import { colorize } from '../utils/colorize';
Expand All @@ -9,6 +10,7 @@ interface JSONLog {
context?: string;
pid: number;
application?: string;
hostname: string;
}

function isNil(val: any): boolean {
Expand All @@ -18,6 +20,7 @@ function isNil(val: any): boolean {
export class Ogma {
private options: OgmaOptions;
private pid: number;
private hostname: string;

public fine = this.verbose;
public log = this.info;
Expand Down Expand Up @@ -50,6 +53,7 @@ export class Ogma {
);
}
this.pid = process.pid;
this.hostname = hostname();
}

private printMessage(
Expand Down Expand Up @@ -123,6 +127,7 @@ export class Ogma {
json.application = application;
}
json.pid = this.pid;
json.hostname = this.hostname;
context = context || this.options.context;
if (context) {
json.context = context;
Expand Down Expand Up @@ -164,7 +169,15 @@ export class Ogma {
this.options.stream,
)} `
: '';
return `${this.wrapInBrackets(this.getTimestamp())} ${application}${
const hostname = `${colorize(
this.wrapInBrackets(this.hostname),
Color.MAGENTA,
this.options.color,
this.options.stream,
)}`;
return `${this.wrapInBrackets(
this.getTimestamp(),
)} ${hostname} ${application}${
this.pid
} ${context}${formattedLevel}| ${message}`;
}
Expand Down
Loading

0 comments on commit a1f2b8d

Please sign in to comment.