Skip to content

Commit

Permalink
Fix LocalStorageAppender removing too many entries on initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgeary committed Oct 11, 2024
1 parent 1680224 commit dbc4c20
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
30 changes: 29 additions & 1 deletion projects/log4ngx/src/lib/appenders/localstorage-appender.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,35 @@ describe('LocalStorageAppender', () => {
expect(logEntries).toBe(message1 + appender.logEntryDelimiter + message2);
});

it('should remove log entries when max days is exceeded', () => {
it('should only remove log entries when > max days exists at initialization', () => {
jasmine.clock().withMock(() => {
const yesterdayTimestamp: number = Date.now();
let appender: LocalStorageAppender = new LocalStorageAppender();
appender.initialize({ ...APPENDER_CONFIG,
maxDays: MAX_DAYS + 1
});

/* Add the max number of entries *prior* to today, so when we reinitialize the appender
for today with reduced `maxDays`, we're left with the correct number of entries.
*/
for (let i: number = appender.maxDays - 1; i >= 0; i--) {
jasmine.clock().mockDate(new Date(yesterdayTimestamp - (i * MILLISECS_PER_DAY)));

const message: string = Random.getString(RANDOM_MESSAGE_LENGTH);
const loggingEvent: LoggingEvent = new LoggingEvent(Level.debug, '', message);
appender.append(loggingEvent);
}

expect(localStorage.length).toBe(appender.maxDays);

/* Now reduce the max days and reinitialize */
appender = new LocalStorageAppender();
appender.initialize(APPENDER_CONFIG);
expect(localStorage.length).toBe(appender.maxDays);
});
});

it('should remove log entries when max days is exceeded on appending new entries', () => {
jasmine.clock().withMock(() => {
const todayTimestamp: number = Date.now();
const appender: LocalStorageAppender = new LocalStorageAppender();
Expand Down
11 changes: 6 additions & 5 deletions projects/log4ngx/src/lib/appenders/localstorage-appender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class LocalStorageAppender extends Appender {

this._localStorage = this.getLocalStorage();
if (this._localStorage !== undefined) {
this.getCurrentLogEntries(this._localStorage); /* Really just to initialize _currentKey and _currentLogEntries */
this.getCurrentLogEntries(this._localStorage, false); /* Really just to initialize _currentKey and _currentLogEntries */
} else {
// eslint-disable-next-line no-console -- there's not much else we can do
console.error('LOG4NGX: LocalStorage is not available; calls to log via LocalStorageAppender will be ignored');
Expand All @@ -68,7 +68,7 @@ export class LocalStorageAppender extends Appender {
if (this._localStorage !== undefined) {
const localStorage: Storage = this._localStorage;
const message: string = this.renderLoggingEvent(loggingEvent);
let currentLogEntries: string = this.getCurrentLogEntries(localStorage);
let currentLogEntries: string = this.getCurrentLogEntries(localStorage, true);

if (currentLogEntries.length === 0) {
currentLogEntries = message;
Expand Down Expand Up @@ -120,13 +120,14 @@ export class LocalStorageAppender extends Appender {
}

/** Gets the current log entries for today, performing any necessary housekeeping on old entries */
private getCurrentLogEntries(localStorage: Storage): string {
private getCurrentLogEntries(localStorage: Storage, appending: boolean): string {
const key: string = this._keyPrefix + new Date().setHours(0, 0, 0, 0)
.toString();

if (key !== this._currentKey) {
/* We're about to add a new key, so need to make sure we have `maxDays`-1 entries at most */
this.removeOldLogKeys(this._maxDays - 1);
/* If we're about to add a new key, so need to make sure we have `maxDays`-1 entries at most */
this.removeOldLogKeys(appending ? this._maxDays - 1
: this._maxDays);

/* We'll reset `_currentLogEntries` just in case there are already log entries for today
from a previous session.
Expand Down

0 comments on commit dbc4c20

Please sign in to comment.