-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
182 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# @ogma/cli | ||
|
||
Rehydration of Ogma JSON logs without a problem. | ||
|
||
Ogma comes with a command line function to rehydrate your json formatted logs back into the human readable format that can be installed with `@ogma/cli`. 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. | ||
|
||
| argument | required | default | description | | ||
| --- | --- | --- | --- | | ||
| file | yes | none | The log file to be "hydrated". This file should contain newline separated Ogma formatted JSON logs. | | ||
| --color | no | terminal's TTY argument | you can pass `--color` or `--color=true` to force colors to be used. `--color=false` will force the command to not print the logs back out in color. Depending on the terminal you are using, colors may not be used by default. | | ||
|
||
The arguments can be passed in any order for ease of use. | ||
|
||
## Example | ||
|
||
An example of the command's usage could be like so: | ||
|
||
```sh | ||
ogma production.log --color | ||
``` | ||
|
||
or if you have a TTY enabled command prompt | ||
|
||
```sh | ||
ogma production.log | ||
``` | ||
|
||
As this prints out to `process.stdout` it is possible to pipe this output to another file using the `>` operator. Like so: | ||
|
||
```sh | ||
ogma production.log > production.hydrated.log | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = { preset: '../../jest.config.js' }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
{ | ||
"name": "@ogma/cli", | ||
"version": "0.4.3", | ||
"description": "A CLI to re-hydrate JSON logs back to a dev readable format, colors included.", | ||
"keywords": [ | ||
"cli", | ||
"ogma", | ||
"logs", | ||
"logger", | ||
"hydrate", | ||
"nodejs" | ||
], | ||
"author": "Jay McDoniel <[email protected]>", | ||
"homepage": "https://github.com/jmcdo29/ogma", | ||
"license": "MIT", | ||
"main": "lib/main.js", | ||
"types": "lib/index.d.ts", | ||
"bin": { | ||
"ogma": "./lib/main.js" | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"directories": { | ||
"lib": "lib" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/jmcdo29/ogma.git" | ||
}, | ||
"scripts": { | ||
"prebuild": "rimraf lib", | ||
"build": "tsc -p tsconfig.build.json", | ||
"postbuild": "mv ./lib/src/* ./lib && rmdir lib/src", | ||
"lint": "eslint --ext .ts ./src/", | ||
"test": "jest", | ||
"test:cov": "jest --coverage" | ||
}, | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/jmcdo29/ogma/issues" | ||
}, | ||
"dependencies": { | ||
"@nestjs/common": "^7.6.5", | ||
"@nestjs/core": "^7.6.5", | ||
"@ogma/logger": "^0.4.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CommandService } from './command.service'; | ||
|
||
@Module({ | ||
providers: [CommandService], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export interface OgmaArgs { | ||
file: string; | ||
color?: boolean; | ||
} | ||
|
||
export const ArgKeyMap = { | ||
'--color': 'color', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { promises } from 'fs'; | ||
import { join } from 'path'; | ||
import { ArgKeyMap, OgmaArgs } from './command-args.interface'; | ||
|
||
const { readFile } = promises; | ||
|
||
@Injectable() | ||
export class CommandService { | ||
private validFlags = Object.keys(ArgKeyMap); | ||
|
||
private ogmaKeys = ['time', 'pid', 'level']; | ||
|
||
async act(args: string[]) { | ||
const options = this.validateArgs(args); | ||
const fileContents = await this.readFile(options.file); | ||
} | ||
|
||
validateArgs(args: string[]): OgmaArgs { | ||
const retArgs: OgmaArgs = { | ||
file: '', | ||
color: process.stdout.isTTY, | ||
}; | ||
retArgs.file = args.filter((arg) => !arg.includes('--'))[0]; | ||
if (!args.length || args.length > 2) { | ||
throw new Error('Invalid usage'); | ||
} | ||
const flags = args.filter((arg) => arg.includes('--')); | ||
const flagKeys = flags.map((flag) => flag.split('=')[0]); | ||
if (args.length > 1 && !flagKeys.every((flag) => this.validFlags.includes(flag))) { | ||
throw new Error('Invalid options'); | ||
} | ||
flags.forEach((flag) => { | ||
const [key, value] = flag.split('='); | ||
retArgs[ArgKeyMap[key]] = value ? JSON.parse(value) : true; | ||
}); | ||
return retArgs; | ||
} | ||
|
||
async readFile(fileName: string): Promise<string> { | ||
const file = await readFile(join(process.cwd(), fileName)); | ||
return file.toString(); | ||
} | ||
|
||
isOgmaFormat(log: Record<string, any>): boolean { | ||
return this.ogmaKeys.every((key) => Object.prototype.hasOwnProperty.call(log, key)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env node | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AppModule } from './app.module'; | ||
import { CommandService } from './command.service'; | ||
|
||
async function bootstrap(...args: string[]) { | ||
const app = await NestFactory.createApplicationContext(AppModule); | ||
const service = app.get(CommandService); | ||
await service.act(args); | ||
await app.close(); | ||
} | ||
|
||
const [, , ...args] = process.argv; | ||
|
||
bootstrap(...args).catch((err) => { | ||
process.stderr.write(err.message); | ||
process.stderr.write(err.stack); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["test"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"rootDir": "./", | ||
"outDir": "./lib" | ||
}, | ||
"include": ["./src", "./test"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters