-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eff5387
commit 37b05c0
Showing
17 changed files
with
520 additions
and
15 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
<p>This page logs a couple of messages to the console from a logger initialized from the component itself.</p> | ||
<p><a [routerLink]="['/']">Click here</a> to go to a page that initializes the logger from a string</p> | ||
<p><a [routerLink]="['/log']">Click here</a> to go to a page that displays the log entries logged to LocalStorage</p> |
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,2 +1,3 @@ | ||
<p>This page logs a couple of messages to the console from a logger initialized from a string (typically the component's name).</p> | ||
<p><a [routerLink]="['/child']">Click here</a> to go to a page that initializes the logger from the component itself</p> | ||
<p><a [routerLink]="['/log']">Click here</a> to go to a page that displays the log entries logged to LocalStorage</p> |
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,7 @@ | ||
<p>This page lists the log entries logged via the LocalStorageAppender.</p> | ||
<p><a [routerLink]="['/']">Click here</a> to go to a page that initializes the logger from a string</p> | ||
<p><a [routerLink]="['/child']">Click here</a> to go to a page that initializes the logger from the component itself</p> | ||
<section *ngFor="let dailyLogs of this.logEntries | keyvalue "> | ||
<h2>{{ dailyLogs.key| date }}</h2> | ||
<p *ngFor="let entry of dailyLogs.value">{{ entry }}</p> | ||
</section> |
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,5 @@ | ||
section { | ||
p { | ||
font-family: 'Courier New', Courier, monospace; | ||
} | ||
} |
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,61 @@ | ||
import { DatePipe, KeyValuePipe, NgFor } from '@angular/common'; | ||
import { ChangeDetectionStrategy, Component, Inject, OnInit } from '@angular/core'; | ||
import { RouterLink } from '@angular/router'; | ||
import { LOCALSTORAGE_APPENDER_TOKEN, LocalStorageAppender, Logger, LogService } from 'log4ngx'; | ||
|
||
@Component({ | ||
selector: 'app-log', | ||
templateUrl: './log.page.html', | ||
styleUrl: './log.page.scss', | ||
changeDetection: ChangeDetectionStrategy.Default, | ||
standalone: true, | ||
imports: [ | ||
DatePipe, | ||
KeyValuePipe, | ||
NgFor, | ||
RouterLink | ||
] | ||
}) | ||
export class LogPage implements OnInit { | ||
public logEntries: Map<Date, string[]> = new Map<Date, string[]>(); | ||
private readonly _log: Logger; | ||
|
||
constructor(@Inject(LOCALSTORAGE_APPENDER_TOKEN) private _localStorageAppender: LocalStorageAppender, | ||
logService: LogService) { | ||
this._log = logService.getLogger(this); | ||
} | ||
|
||
public ngOnInit(): void { | ||
const storage: Storage = window['localStorage']; | ||
const logKeys: string[] = this.getLogKeys(storage); | ||
const prefix: string = this._localStorageAppender.keyPrefix; | ||
|
||
this.logEntries = new Map<Date, string[]>(); | ||
|
||
for (const key of logKeys) { | ||
const logEntries: string | null = storage.getItem(key); | ||
|
||
if (logEntries !== null) { | ||
const timestamp: number = Number.parseInt(key.slice(prefix.length)); | ||
const date: Date = new Date(timestamp); | ||
const entries: string[] = logEntries.split(this._localStorageAppender.logEntryDelimiter); | ||
|
||
this.logEntries.set(date, entries); | ||
} | ||
} | ||
} | ||
|
||
private getLogKeys(localStorage: Storage): string[] { | ||
const keyPrefix: string = this._localStorageAppender.keyPrefix; | ||
const logKeys: string[] = []; | ||
|
||
for (let i: number = localStorage.length - 1; i >= 0; i--) { | ||
const key: string | null = localStorage.key(i); | ||
if (key !== null && key.startsWith(keyPrefix)) { | ||
logKeys.push(key); | ||
} | ||
} | ||
|
||
return logKeys; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
export * from './appender-config'; | ||
export * from './appender'; | ||
export * from './console-appender-config'; | ||
export * from './appender-config'; | ||
export * from './console-appender'; | ||
export * from './console-appender-config'; | ||
export * from './localstorage-appender'; | ||
export * from './localstorage-appender-config'; | ||
export * from './mock-appender'; | ||
export * from './mockatoo-appender'; |
11 changes: 11 additions & 0 deletions
11
projects/log4ngx/src/lib/appenders/localstorage-appender-config.ts
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,11 @@ | ||
import { AppenderConfig } from './appender-config'; | ||
|
||
/** Interface defining the configuration of `LocalStorageAppender` objects. | ||
* @extends AppenderConfig | ||
*/ | ||
export interface LocalStorageAppenderConfig extends AppenderConfig { | ||
/** Defines the prefix used for the key when adding items to `localStorage`. */ | ||
keyPrefix?: string; | ||
logEntryDelimiter?: string; | ||
maxDays?: number; | ||
} |
Oops, something went wrong.