Skip to content

Commit

Permalink
feat(logger): move log level to earlier in the log
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Oct 26, 2020
1 parent 592bbcc commit da00caa
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 37 deletions.
14 changes: 4 additions & 10 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, { context: SomeClass.name, application: 'NestJS' })` and Ogma will print

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

> 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] [hostname] [Application] PID [Context] [LogLevel]| message
[ISOString Date] [logLevel] [hostname] [Application] PID [Context] 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] [hostname] 34760 [INFO] | hello
[2019-12-11T22:54:58.462Z] [INFO] [hostname] 34760 hello

# ogma.log({a: () => 'hello', b: {c: 'nested'}, d: this});
[2019-12-11T22:56:02.502Z] [hostname] 34760 [INFO] |
[2019-12-11T22:56:02.502Z] [INFO] [hostname] 34760
{
"a": "[Function]",
"b": {
Expand Down Expand Up @@ -149,12 +149,6 @@ I said the logs were beautiful, and to me they absolutely are. Each log is match
}
```

### Example from Command Line

![](ogma_sample.png)

![](ogma_sample_json.png)

## Command Line Function

Ogma comes with a built in command line function to rehydrate your json formatted logs back into the human readable format. The command takes one to two arguments, a log file relative to where the command is run from, and an optional flag to force the cli to print out with color. Find the table below to learn more about the arguments.
Expand Down
Binary file removed packages/logger/ogma_sample.png
Binary file not shown.
Binary file removed packages/logger/ogma_sample_json.png
Binary file not shown.
3 changes: 1 addition & 2 deletions packages/logger/src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function writeLog(log: OgmaLog, useColor: boolean): void {
message = getMessageFromJSON(rest);
}
let logMessage = wrapInParens(time) + ' ';
logMessage += getLevel(level, useColor) + ' ';
logMessage += getHostname(hostname, useColor) + ' ';
if (application) {
logMessage += getApplication(application, useColor) + ' ';
Expand All @@ -117,8 +118,6 @@ function writeLog(log: OgmaLog, useColor: boolean): void {
if (context) {
logMessage += getContext(context, useColor) + ' ';
}
logMessage += getLevel(level, useColor);
logMessage += '| ';
logMessage += message + '\n';
process.stdout.write(Buffer.from(logMessage));
}
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/logger/ogma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class Ogma {

const hostname = this.toStreamColor(this.hostname, Color.MAGENTA);
const timestamp = this.wrapInBrackets(this.getTimestamp());
return `${timestamp} ${hostname} ${application} ${this.pid} ${correlationId}${context}${formattedLevel}| ${message}`;
return `${timestamp} ${formattedLevel} ${hostname} ${application} ${this.pid} ${correlationId} ${context} ${message}`;
}

private toStreamColor(value: string, color: Color): string {
Expand Down
44 changes: 22 additions & 22 deletions packages/logger/test/command.fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const fullJSON: OgmaLogSet = {
};

function hydrateNoAppNoConFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${pid} ${level}| ${JSON.stringify(hello)}\n`;
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${pid} ${JSON.stringify(hello)}\n`;
}

const hydratedNoAppNoCon: ExpectedOgmaOutput = {
Expand All @@ -100,9 +100,9 @@ const hydratedNoAppNoCon: ExpectedOgmaOutput = {
};

function hydrateNoConFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${color.yellow(
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${color.yellow(
'[' + application + ']',
)} ${pid} ${level}| ${JSON.stringify(hello)}\n`;
)} ${pid} ${JSON.stringify(hello)}\n`;
}

const hydratedNoCon: ExpectedOgmaOutput = {
Expand All @@ -116,9 +116,9 @@ const hydratedNoCon: ExpectedOgmaOutput = {
};

function hydrateNoAppFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${pid} ${color.cyan(
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${pid} ${color.cyan(
'[' + context + ']',
)} ${level}| ${JSON.stringify(hello)}\n`;
)} ${JSON.stringify(hello)}\n`;
}

const hydratedNoApp: ExpectedOgmaOutput = {
Expand All @@ -132,9 +132,9 @@ const hydratedNoApp: ExpectedOgmaOutput = {
};

function hydrateFullFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${color.yellow(
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${color.yellow(
'[' + application + ']',
)} ${pid} ${color.cyan('[' + context + ']')} ${level}| ${JSON.stringify(hello)}\n`;
)} ${pid} ${color.cyan('[' + context + ']')} ${JSON.stringify(hello)}\n`;
}

const hydratedFull: ExpectedOgmaOutput = {
Expand All @@ -148,7 +148,7 @@ const hydratedFull: ExpectedOgmaOutput = {
};

function hydrateNoAppNoConNoColorFactory(level: string): string {
return `[${time}] [${host}] ${pid} ${level}| ${JSON.stringify(hello)}\n`;
return `[${time}] ${level} [${host}] ${pid} ${JSON.stringify(hello)}\n`;
}

const hydratedNoAppNoConNoColor: ExpectedOgmaOutput = {
Expand All @@ -162,7 +162,7 @@ const hydratedNoAppNoConNoColor: ExpectedOgmaOutput = {
};

function hydrateNoConNoColorFactory(level: string): string {
return `[${time}] [${host}] [${application}] ${pid} ${level}| ${JSON.stringify(hello)}\n`;
return `[${time}] ${level} [${host}] [${application}] ${pid} ${JSON.stringify(hello)}\n`;
}

const hydratedNoConNoColor: ExpectedOgmaOutput = {
Expand All @@ -176,7 +176,7 @@ const hydratedNoConNoColor: ExpectedOgmaOutput = {
};

function hydrateNoAppNoColorFactory(level: string): string {
return `[${time}] [${host}] ${pid} [${context}] ${level}| ${JSON.stringify(hello)}\n`;
return `[${time}] ${level} [${host}] ${pid} [${context}] ${JSON.stringify(hello)}\n`;
}

const hydratedNoAppNoColor: ExpectedOgmaOutput = {
Expand All @@ -190,7 +190,7 @@ const hydratedNoAppNoColor: ExpectedOgmaOutput = {
};

function hydrateFullNoColorFactory(level: string): string {
return `[${time}] [${host}] [${application}] ${pid} [${context}] ${level}| ${JSON.stringify(
return `[${time}] ${level} [${host}] [${application}] ${pid} [${context}] ${JSON.stringify(
hello,
)}\n`;
}
Expand Down Expand Up @@ -273,7 +273,7 @@ const fullString: OgmaLogSet = {
};

function hydrateNoAppNoConStringFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${pid} ${level}| ${message}\n`;
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${pid} ${message}\n`;
}

const hydratedNoAppNoConString: ExpectedOgmaOutput = {
Expand All @@ -287,9 +287,9 @@ const hydratedNoAppNoConString: ExpectedOgmaOutput = {
};

function hydrateNoConStringFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${color.yellow(
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${color.yellow(
'[' + application + ']',
)} ${pid} ${level}| ${message}\n`;
)} ${pid} ${message}\n`;
}

const hydratedNoConString: ExpectedOgmaOutput = {
Expand All @@ -303,9 +303,9 @@ const hydratedNoConString: ExpectedOgmaOutput = {
};

function hydrateNoAppStringFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${pid} ${color.cyan(
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${pid} ${color.cyan(
'[' + context + ']',
)} ${level}| ${message}\n`;
)} ${message}\n`;
}

const hydratedNoAppString: ExpectedOgmaOutput = {
Expand All @@ -319,9 +319,9 @@ const hydratedNoAppString: ExpectedOgmaOutput = {
};

function hydrateFullStringFactory(level: string): string {
return `[${time}] ${color.magenta('[' + host + ']')} ${color.yellow(
return `[${time}] ${level} ${color.magenta('[' + host + ']')} ${color.yellow(
'[' + application + ']',
)} ${pid} ${color.cyan('[' + context + ']')} ${level}| ${message}\n`;
)} ${pid} ${color.cyan('[' + context + ']')} ${message}\n`;
}

const hydratedFullString: ExpectedOgmaOutput = {
Expand All @@ -335,7 +335,7 @@ const hydratedFullString: ExpectedOgmaOutput = {
};

function hydrateNoAppNoConNoColorStringFactory(level: string): string {
return `[${time}] [${host}] ${pid} ${level}| ${message}\n`;
return `[${time}] ${level} [${host}] ${pid} ${message}\n`;
}

const hydratedNoAppNoConNoColorString: ExpectedOgmaOutput = {
Expand All @@ -349,7 +349,7 @@ const hydratedNoAppNoConNoColorString: ExpectedOgmaOutput = {
};

function hydrateNoConNoColorStringFactory(level: string): string {
return `[${time}] [${host}] [${application}] ${pid} ${level}| ${message}\n`;
return `[${time}] ${level} [${host}] [${application}] ${pid} ${message}\n`;
}

const hydratedNoConNoColorString: ExpectedOgmaOutput = {
Expand All @@ -363,7 +363,7 @@ const hydratedNoConNoColorString: ExpectedOgmaOutput = {
};

function hydrateNoAppNoColorStringFactory(level: string): string {
return `[${time}] [${host}] ${pid} [${context}] ${level}| ${message}\n`;
return `[${time}] ${level} [${host}] ${pid} [${context}] ${message}\n`;
}

const hydratedNoAppNoColorString: ExpectedOgmaOutput = {
Expand All @@ -377,7 +377,7 @@ const hydratedNoAppNoColorString: ExpectedOgmaOutput = {
};

function hydrateFullNoColorStringFactory(level: string): string {
return `[${time}] [${host}] [${application}] ${pid} [${context}] ${level}| ${message}\n`;
return `[${time}] ${level} [${host}] [${application}] ${pid} [${context}] ${message}\n`;
}

const hydratedFullNoColorString: ExpectedOgmaOutput = {
Expand Down
4 changes: 2 additions & 2 deletions packages/logger/test/ogma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ describe('Ogma Class', () => {

describe.each`
color | expectation
${true} | ${Color.cyan('[INFO] ') + '| Hello'}
${false} | ${'[INFO] | Hello'}
${true} | ${Color.cyan('[INFO] ')}
${false} | ${'[INFO] '}
`('color: $color', ({ color, expectation }: { color: boolean; expectation: string }) => {
beforeEach(() => {
ogma = createOgmaInstance({
Expand Down

0 comments on commit da00caa

Please sign in to comment.