-
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.
feat: Introduce a logging context which holds logs and source lines w…
…hich allows for log messages to include a summary of the source code which resulted in the log message
- Loading branch information
1 parent
88b9a10
commit eb48a3a
Showing
9 changed files
with
151 additions
and
86 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
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,44 @@ | ||
import chalk from 'chalk' | ||
import type { SourceLocation } from '../../awst/source-location' | ||
import type { LogEvent, LogLevel } from '../index' | ||
import { LoggingContext } from '../index' | ||
import type { LogSink } from './index' | ||
|
||
type ColorFn = (text: string) => string | ||
const levelConfig: Record<LogEvent['level'], { colorFn: ColorFn; writeFn: (...args: unknown[]) => void }> = { | ||
/* eslint-disable no-console */ | ||
debug: { colorFn: chalk.green, writeFn: console.debug }, | ||
info: { colorFn: chalk.green, writeFn: console.info }, | ||
warn: { colorFn: chalk.yellow, writeFn: console.warn }, | ||
error: { colorFn: chalk.red, writeFn: console.error }, | ||
critical: { colorFn: chalk.red, writeFn: console.error }, | ||
/* eslint-enable no-console */ | ||
} | ||
|
||
export class ConsoleLogSink implements LogSink { | ||
constructor(public readonly minLogLevel: LogLevel) {} | ||
|
||
add(logEvent: LogEvent): void { | ||
const config = levelConfig[logEvent.level] | ||
|
||
let logText = `${config.colorFn(logEvent.level)}: ${logEvent.message}` | ||
if (logEvent.sourceLocation) { | ||
const sourceLocationText = logEvent.sourceLocation.toString() | ||
const indentSize = sourceLocationText.length + logEvent.level.length + 4 | ||
|
||
logText = `${sourceLocationText} ${logText}${this.getSourceSummary(logEvent.sourceLocation, indentSize)}` | ||
} | ||
config.writeFn(logText) | ||
} | ||
|
||
getSourceSummary(sourceLocation: SourceLocation, indent: number): string { | ||
const sourceFile = LoggingContext.current.sourcesByPath[sourceLocation.file] | ||
if (!sourceFile) return '' | ||
|
||
const line = sourceFile[sourceLocation.line - 1] | ||
const trimmedLine = line.trimStart() | ||
const marker = `${''.padStart(sourceLocation.column - (line.length - trimmedLine.length))}^${''.padStart(Math.max(sourceLocation.endColumn - sourceLocation.column - 1, 0), '~')}` | ||
const indentChars = ''.padStart(indent, ' ') | ||
return `\n${indentChars}${trimmedLine}\n${indentChars}${marker}` | ||
} | ||
} |
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,6 @@ | ||
import type { LogEvent, LogLevel } from '../index' | ||
|
||
export type LogSink = { | ||
readonly minLogLevel: LogLevel | ||
add(e: LogEvent): void | ||
} |
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.