Skip to content

Commit

Permalink
Add logging section header name
Browse files Browse the repository at this point in the history
  • Loading branch information
luigicerone-sso committed Sep 26, 2024
1 parent f56bcc3 commit 29ca0f8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ For those requiring more detailed logging, consider setting up a custom [log tem
- 🥤 (pomodoro::BREAK) (duration:: 25m) (begin:: 2023-12-20 16:06) - (end:: 2023-12-20 16:07)
```


### Custom Section Header (Optional)

By default, logs are appended to the end of the file specified in the settings.

If you want to append logs under a specific section header within the file, you can use the "Log section header name" setting. Simply copy and paste the unique section name as plain markdown syntax, including the "#" symbols (e.g., "## Logs header").


### Custom Log Template (Optional)

1. Install the [Templater](https://github.com/SilentVoid13/Templater) plugin.
Expand Down
35 changes: 33 additions & 2 deletions src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,49 @@ export default class Logger {
}

public async log(ctx: LogContext): Promise<TFile | void> {
const settings = this.plugin!.getSettings()

const logFile = await this.resolveLogFile(ctx)
const log = this.createLog(ctx)
if (logFile) {
const logText = await this.toText(log, logFile)
if (logText) {
await this.plugin.app.vault.append(logFile, `\n${logText}`)
if (logText && settings.logSectionName) {
const fileContent = await this.plugin.app.vault.read(logFile);
const headerPosition = await this.findHeaderInFile(fileContent, settings.logSectionName)

const updatedText = await this.updateFileContent(logText, fileContent, headerPosition)
await this.plugin.app.vault.modify(logFile, updatedText);
}
else if (logText && !settings.logSectionName) {
await this.plugin.app.vault.append(logFile, `\n${logText}`)
}
}

return logFile
}

private async updateFileContent(logText: string, fileContent: string, headerPosition: number) {
return fileContent.slice(0, headerPosition) + `${logText}\n` + fileContent.slice(headerPosition);
}

private async findHeaderInFile(fileContent: string, logSectionName: string) {
const headerPosition = fileContent.indexOf(logSectionName);

if (headerPosition == -1) {
new Notice('Section name not found inside the log file!')
}

// Find the start of the next section (next header at the same or higher level)
const nextHeaderRegex = /^(#{1,6})\s+/gm; // Match any header from # to ######
nextHeaderRegex.lastIndex = headerPosition + logSectionName.length;

// Find the next header after the current section
const nextHeaderMatch = nextHeaderRegex.exec(fileContent);

// Determine the end of the section
return nextHeaderMatch ? nextHeaderMatch.index : fileContent.length;
}

private async resolveLogFile(ctx: LogContext): Promise<TFile | void> {
const settings = this.plugin!.getSettings()

Expand Down
13 changes: 13 additions & 0 deletions src/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Settings {
logFile: LogFileType
logFocused: boolean
logPath: string
logSectionName: string
logLevel: LogLevel
logTemplate: string
logFormat: LogFormat
Expand All @@ -47,6 +48,7 @@ export default class PomodoroSettings extends PluginSettingTab {
logFile: 'NONE',
logFocused: false,
logPath: '',
logSectionName: '',
logLevel: 'ALL',
logTemplate: '',
logFormat: 'VERBOSE',
Expand Down Expand Up @@ -228,6 +230,17 @@ export default class PomodoroSettings extends PluginSettingTab {
})
}

new Setting(containerEl)
.setName('Log section header name')
.setDesc('The section name under which log entries are organized (including # signs)')
.addText((text) => {
text.inputEl.style.width = '300px'
text.setValue(this._settings.logSectionName)
text.onChange((value) => {
this.updateSettings({ logSectionName: value })
})
});

new Setting(containerEl)
.setName('Log Level')
.addDropdown((dropdown) => {
Expand Down

0 comments on commit 29ca0f8

Please sign in to comment.