-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add user-agent parser and introduce new env variables (#122)
* refactor: add user-agent parser and introduce new env variables * feat: add experimental logtail logger and improve general logger with new options * chore(refactor): auto format Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
Showing
19 changed files
with
223 additions
and
72 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,48 @@ | ||
import { SummaryMeasureFormatter } from 'x/optic/profiler'; | ||
import { ConsoleStream, Level, Logger } from 'x/optic'; | ||
import { TokenReplacer } from 'x/optic/formatters'; | ||
//import { RegExpFilter } from 'x/optic/regex-filter'; | ||
import { LogtailStream } from '../logger/logtail.ts'; | ||
import { env } from './env.ts'; | ||
import { MinLogLevel } from '../logger/types.d.ts'; | ||
|
||
//const regex = RegExp(/[&?]+/); | ||
//const regExpFilter = new RegExpFilter(regex); | ||
export const logger = new Logger(); //.addFilter(regExpFilter); | ||
const consoleLogger = new ConsoleStream() | ||
.withFormat( | ||
new TokenReplacer() | ||
.withFormat('{msg} {metadata}') | ||
.withColor(), | ||
); | ||
|
||
const betterStackLogger = new LogtailStream( | ||
env<string>('LOGTAIL_KEY'), | ||
); | ||
|
||
const logLevel = (level: MinLogLevel): Level => { | ||
switch (level) { | ||
case 'DEBUG': | ||
return Level.Debug; | ||
case 'INFO': | ||
return Level.Info; | ||
case 'WARN': | ||
return Level.Warn; | ||
case 'ERROR': | ||
return Level.Error; | ||
default: | ||
throw new Error('Unkown log level', { cause: level }); | ||
} | ||
}; | ||
|
||
const logger = new Logger() | ||
.withMinLogLevel( | ||
logLevel(env<MinLogLevel>('MIN_LOG_LEVEL')), | ||
) | ||
.addStream(consoleLogger) | ||
.addStream(betterStackLogger); | ||
|
||
logger.profilingConfig() | ||
.enabled(true) | ||
.enabled(env<boolean>('OPTIC_TRACING')) | ||
.captureMemory(true) | ||
.captureOps(true) | ||
.withLogLevel(Level.Info) | ||
.withFormatter(new SummaryMeasureFormatter()); | ||
|
||
logger.addStream( | ||
new ConsoleStream() | ||
.withFormat( | ||
new TokenReplacer() | ||
.withFormat('{msg} {metadata}') | ||
.withColor(), | ||
), | ||
); | ||
export { logger }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Logtail } from 'esm/logtail'; | ||
import { TokenReplacer } from 'x/optic/formatters'; | ||
import { BaseStream, Level, LogRecord } from 'x/optic'; | ||
|
||
export class LogtailStream extends BaseStream { | ||
private logtail: Logtail; | ||
|
||
constructor(clientKey: string) { | ||
super(new TokenReplacer()); | ||
this.logtail = new Logtail(clientKey); | ||
} | ||
|
||
log(msg: string): void { | ||
this.logtail.log(msg); | ||
this.logtail.flush(); | ||
} | ||
|
||
override handle(logRecord: LogRecord): boolean { | ||
const { level, metadata } = logRecord; | ||
if (this.minLevel > level) return false; | ||
const msg = this.format(logRecord); | ||
|
||
switch (level) { | ||
case Level.Info: | ||
this.logtail.info(msg, { ...metadata }); | ||
break; | ||
case Level.Warn: | ||
this.logtail.warn(msg, { ...metadata }); | ||
break; | ||
case Level.Error: | ||
this.logtail.error(msg, { ...metadata }); | ||
break; | ||
case Level.Critical: | ||
this.logtail.log(msg, 'fatal', { ...metadata }); | ||
break; | ||
default: | ||
return false; | ||
} | ||
|
||
this.logtail.flush(); | ||
return true; | ||
} | ||
} |
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 @@ | ||
export type MinLogLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR'; |
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
Oops, something went wrong.