-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic tracing based on the console logging and printed SQL queries
Later on it'll be replaced with real Open Telemetry.
- Loading branch information
1 parent
0e27f26
commit 9b8421a
Showing
3 changed files
with
93 additions
and
0 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
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,90 @@ | ||
import { JSONSerializer } from '../serializer'; | ||
|
||
export const tracer = () => {}; | ||
|
||
export type LogLevel = 'DISABLED' | 'INFO' | 'LOG' | 'WARN' | 'ERROR'; | ||
export const LogLevel = { | ||
DISABLED: 'DISABLED' as LogLevel, | ||
INFO: 'INFO' as LogLevel, | ||
LOG: 'LOG' as LogLevel, | ||
WARN: 'WARN' as LogLevel, | ||
ERROR: 'ERROR' as LogLevel, | ||
}; | ||
|
||
const shouldLog = (logLevel: LogLevel): boolean => { | ||
const definedLogLevel = process.env.PONGO_LOG_LEVEL ?? LogLevel.DISABLED; | ||
|
||
if (definedLogLevel === LogLevel.ERROR && logLevel === LogLevel.ERROR) | ||
return true; | ||
|
||
if ( | ||
definedLogLevel === LogLevel.WARN && | ||
[LogLevel.ERROR, LogLevel.WARN].includes(logLevel) | ||
) | ||
return true; | ||
|
||
if ( | ||
definedLogLevel === LogLevel.LOG && | ||
[LogLevel.ERROR, LogLevel.WARN, LogLevel.LOG].includes(logLevel) | ||
) | ||
return true; | ||
|
||
if ( | ||
definedLogLevel === LogLevel.INFO && | ||
[LogLevel.ERROR, LogLevel.WARN, LogLevel.INFO].includes(logLevel) | ||
) | ||
return true; | ||
|
||
return false; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
tracer.log = (eventName: string, attributes?: Record<string, any>) => { | ||
if (!shouldLog(LogLevel.LOG)) return; | ||
|
||
console.log( | ||
JSONSerializer.serialize({ | ||
name: eventName, | ||
timestamp: new Date().getTime(), | ||
...attributes, | ||
}), | ||
); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
tracer.warn = (eventName: string, attributes?: Record<string, any>) => { | ||
if (!shouldLog(LogLevel.WARN)) return; | ||
|
||
console.warn( | ||
JSONSerializer.serialize({ | ||
name: eventName, | ||
timestamp: new Date().getTime(), | ||
...attributes, | ||
}), | ||
); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
tracer.error = (eventName: string, attributes?: Record<string, any>) => { | ||
if (!shouldLog(LogLevel.ERROR)) return; | ||
|
||
console.error( | ||
JSONSerializer.serialize({ | ||
name: eventName, | ||
timestamp: new Date().getTime(), | ||
...attributes, | ||
}), | ||
); | ||
}; | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
tracer.info = (eventName: string, attributes?: Record<string, any>) => { | ||
if (!shouldLog(LogLevel.INFO)) return; | ||
|
||
console.info( | ||
JSONSerializer.serialize({ | ||
name: eventName, | ||
timestamp: new Date().getTime(), | ||
...attributes, | ||
}), | ||
); | ||
}; |
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