Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | 29x 154x 53x 154x 1474x | import type { SyncFinalizer } from '../implementation/finalizer.ts';
import type { LogEntry } from '../log-entry.ts';
import { asLogEntry } from '../log-entry.ts';
import type { LogSink } from './sink.ts';
/**
* Interface for accessing stored log entries and clearing the memory store.
*/
export type MemoryStore = {
/**
* The log entries stored in memory.
*/
readonly entries: readonly LogEntry[];
/**
* Clears the log entries stored in memory.
*/
readonly flush: () => void;
};
/**
* A log sink that stores entries in memory with synchronous flush/close operations.
*/
export type MemorySink = SyncFinalizer<LogSink> & MemoryStore;
/**
* Creates a memory-based log sink that stores entries in an array.
*
* This sink is primarily useful for testing, debugging, or applications that need to inspect logged entries
* programmatically. All entries are kept in memory until explicitly cleared.
*
* @example Basic usage
*
* ```ts
* import { emitter } from 'emitnlog/logger';
*
* const memory = emitter.memorySink();
* const logger = emitter.createLogger('info', memory);
*
* logger.i`Application started`;
* logger.e`Something failed`;
*
* console.log(memory.entries); // Array with 2 log entries
* memory.clear(); // Remove all entries
* ```
*
* @example With pre-existing entries array
*
* ```ts
* import type { LogEntry } from 'emitnlog/logger';
* import { emitter } from 'emitnlog/logger';
*
* const existingEntries: LogEntry[] = [];
* const memory = emitter.memorySink(existingEntries);
* // Now both memory.entries and existingEntries reference the same array
* ```
*
* @param entries Optional array to use for storing entries (default: new empty array)
* @returns A MemorySink that stores log entries in memory
*/
export const memorySink = (entries: LogEntry[] = []): MemorySink => {
const flush = () => {
entries.length = 0;
};
return {
sink: (level, message, args) => {
entries.push(asLogEntry(level, message, args));
},
entries,
flush,
close: flush,
};
};
|