-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IDE's logging to a file #6478
Merged
Merged
IDE's logging to a file #6478
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
24c6853
wip
mwu-tow 632c72d
Refactor log.ts to use more descriptive function names
mwu-tow c3a443d
Merge remote-tracking branch 'origin/develop' into wip/mwu/ide-logs
mwu-tow d6c81fc
wip
mwu-tow e490fc6
Merge remote-tracking branch 'origin/develop' into wip/mwu/ide-logs
mwu-tow 8281002
CR
mwu-tow 3626942
Merge remote-tracking branch 'origin/develop' into wip/mwu/ide-logs
mwu-tow e51c05c
Merge remote-tracking branch 'origin/develop' into wip/mwu/ide-logs
mwu-tow 422e72c
cleanups
mwu-tow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** @file Logging utilities. | ||
* | ||
* This module includes a special {@link addFileLog function} that adds a new log consumer that writes to a file. | ||
* | ||
* This is the primary entry point, though its building blocks are also exported, like {@link FileConsumer}. */ | ||
|
||
import * as fsSync from 'node:fs' | ||
import * as pathModule from 'node:path' | ||
|
||
import * as contentConfig from 'enso-content-config' | ||
import * as paths from 'paths' | ||
|
||
import * as linkedDist from '../../../../../target/ensogl-pack/linked-dist' | ||
|
||
// ================ | ||
// === Log File === | ||
// ================ | ||
|
||
/** Adds a new log consumer that writes to a file. | ||
* | ||
* The path of the log file is {@link generateUniqueLogFileName automatically generated}. | ||
* | ||
* The log file is created in the {@link paths.LOGS_DIRECTORY logs directory} | ||
* | ||
* @returns The full path of the log file. */ | ||
export function addFileLog(): string { | ||
const dirname = paths.LOGS_DIRECTORY | ||
const filename = generateUniqueLogFileName() | ||
const logFilePath = pathModule.join(dirname, filename) | ||
const consumer = new FileConsumer(logFilePath) | ||
contentConfig.logger.addConsumer(consumer) | ||
return logFilePath | ||
} | ||
|
||
/** Generate a unique log file name based on the current timestamp. | ||
* | ||
* @returns The file name log file. */ | ||
export function generateUniqueLogFileName(): string { | ||
// Replace ':' with '-' because ':' is not allowed in file names. | ||
const timestamp = new Date().toISOString().replace(/:/g, '-') | ||
const version = contentConfig.VERSION.ide.raw | ||
return `${timestamp}-ide-${version}.log` | ||
} | ||
|
||
// ================ | ||
// === Consumer === | ||
// ================ | ||
|
||
/** Log consumer that writes to a file. */ | ||
export class FileConsumer extends linkedDist.Consumer { | ||
private readonly logFilePath: string | ||
private readonly logFileHandle: number | ||
|
||
/** Create a log consumer that writes to a file. | ||
* | ||
* @param logPath - The path of the log file. Must be writeable. */ | ||
constructor(logPath: string) { | ||
super() | ||
// Create the directory if it doesn't exist, otherwise fsSync.openSync will fail. | ||
const logsDirectory = pathModule.dirname(logPath) | ||
fsSync.mkdirSync(logsDirectory, { recursive: true }) | ||
this.logFilePath = logPath | ||
this.logFileHandle = fsSync.openSync(this.logFilePath, 'a') | ||
} | ||
|
||
override message(level: linkedDist.LogLevel, ...args: unknown[]): void { | ||
const timestamp = new Date().toISOString() | ||
const message = args | ||
.map(arg => (typeof arg === 'string' ? arg : JSON.stringify(arg))) | ||
.join(' ') | ||
const timestampedMessage = `[${timestamp}] [${level.toUpperCase()}] ${message}\n` | ||
|
||
if (this.logFileHandle) { | ||
try { | ||
fsSync.writeSync(this.logFileHandle, timestampedMessage) | ||
} catch (error) { | ||
console.error('Failed to write log:', error) | ||
} | ||
} else { | ||
// This should never happen, as the log file handle is initialized in the constructor. | ||
console.error('Log file not initialized.') | ||
} | ||
} | ||
|
||
override startGroup(...args: unknown[]): void { | ||
this.message('log', '[GROUP START]', ...args) | ||
} | ||
|
||
override startGroupCollapsed(...args: unknown[]): void { | ||
// We don't have a way to collapse groups in the file logger, so we just use the same | ||
// function as startGroup. | ||
this.message('log', '[GROUP START]', ...args) | ||
} | ||
|
||
override groupEnd(...args: unknown[]): void { | ||
this.message('log', '[GROUP END]', ...args) | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sections