Class RotatingFileHandler
Hierarchy
- FileHandler
- RotatingFileHandler (view full)
Constructors
constructor
- new
Rotating (levelName, options): RotatingFileHandlerFile Handler Parameters
- levelName: LevelName
- options: RotatingFileHandlerOptions
Returns RotatingFileHandler
Properties
formatter
The function that formats log records.
Example: Usage
import { BaseHandler } from "@std/log/base-handler";
import { LogRecord } from "@std/log/logger";
import { LogLevels } from "@std/log/levels";
import { assertEquals } from "@std/assert/equals";
class MyHandler extends BaseHandler {
log(msg: string) {
console.log(msg);
}
}
const handler = new MyHandler("INFO");
const record = new LogRecord({
msg: "Hello, world!",
args: ["foo", "bar"],
level: LogLevels.INFO,
loggerName: "example",
});
const formatted = handler.formatter(record);
assertEquals(formatted, "INFO Hello, world!");
Accessors
level
- get level(): LogLevel
Getter for the log level that this handler will handle.
Returns LogLevel
The log level to handle.
Example: Usage
import { BaseHandler } from "@std/log/base-handler";
import { LogLevels } from "@std/log/levels";
import { assertEquals } from "@std/assert/equals";
class MyHandler extends BaseHandler {
log(msg: string) {
console.log(msg);
}
}
const handler = new MyHandler("INFO");
assertEquals(handler.level, LogLevels.INFO);- set level(level): void
Setter for the log level that this handler will handle.
Parameters
- level: LogLevel
The log level to handle.
Returns void
Example: Usage
import { BaseHandler } from "@std/log/base-handler";
import { LogLevels } from "@std/log/levels";
import { assertEquals } from "@std/assert/equals";
class MyHandler extends BaseHandler {
log(msg: string) {
console.log(msg);
}
}
const handler = new MyHandler("INFO");
handler.level = LogLevels.DEBUG;
assertEquals(handler.level, LogLevels.DEBUG);- level: LogLevel
levelName
- get levelName(): LevelName
Getter for the name of the log level that this handler will handle.
Returns LevelName
The name of the log level to handle.
- set levelName(levelName): void
Setter for the name of the log level that this handler will handle.
Parameters
- levelName: LevelName
The name of the log level to handle.
Returns void
Example: Usage
import { BaseHandler } from "@std/log/base-handler";
import { assertEquals } from "@std/assert/equals";
class MyHandler extends BaseHandler {
log(msg: string) {
console.log(msg);
}
}
const handler = new MyHandler("INFO");
handler.levelName = "DEBUG";
assertEquals(handler.levelName, "DEBUG");- levelName: LevelName
Methods
[dispose]
- [dispose](): void
Automatically disposes of the handler when instantiated with the
usingkeyword by calling theBaseHandler.destroymethod.Returns void
Example: Usage
import { BaseHandler } from "@std/log/base-handler";
import { LogRecord } from "@std/log/logger";
import { assertInstanceOf } from "@std/assert/instance-of";
class MyHandler extends BaseHandler {
log(msg: string) {
console.log(msg);
}
}
using handler = new MyHandler("INFO");
assertInstanceOf(handler, BaseHandler);
destroy
flush
- flush(): void
Immediately writes the contents of the buffer to the previously opened file.
Returns void
Example: Usage
import { FileHandler } from "@std/log/file-handler";
import { assertInstanceOf } from "@std/assert/instance-of";
const handler = new FileHandler("INFO", { filename: "./_tmp/logs.txt" });
handler.setup();
handler.log('Hello, world!');
handler.flush(); // Writes buffered log messages to the file immediately.
handler.destroy();
assertInstanceOf(handler, FileHandler);
format
- format(logRecord): string
Formats a log record.
Parameters
- logRecord: LogRecord
The log record to format.
Returns string
A string representation of the log record.
Example: Usage
import { BaseHandler } from "@std/log/base-handler";
import { LogRecord } from "@std/log/logger";
import { LogLevels } from "@std/log/levels";
import { assertEquals } from "@std/assert/equals";
class MyHandler extends BaseHandler {
log(msg: string) {
console.log(msg);
}
}
const handler = new MyHandler("INFO");
const record = new LogRecord({
msg: "Hello, world!",
args: ["foo", "bar"],
level: LogLevels.INFO,
loggerName: "example",
});
const formatted = handler.format(record);
assertEquals(formatted, "INFO Hello, world!");- logRecord: LogRecord
handle
- handle(logRecord): void
Handles a log record and flushes the buffer if the log level is higher than error.
Parameters
- logRecord: LogRecord
Log record to handle.
Returns void
Example: Usage
import { FileHandler } from "@std/log/file-handler";
import { assertInstanceOf } from "@std/assert/instance-of";
import { LogLevels } from "./levels.ts";
import { LogRecord } from "./logger.ts";
const handler = new FileHandler("INFO", { filename: "./_tmp/logs.txt" });
handler.setup();
// Flushes the buffer immediately and logs "CRITICAL This log is very critical indeed." into the file.
handler.handle(
new LogRecord({
msg: "This log is very critical indeed.",
args: [],
level: LogLevels.CRITICAL,
loggerName: "INFO",
}),
);
handler.destroy();
assertInstanceOf(handler, FileHandler);- logRecord: LogRecord
log
- log(msg): void
Logs a message by adding it to the buffer, with flushing as needed.
Parameters
- msg: string
The message to log.
Returns void
Example: Usage
import { FileHandler } from "@std/log/file-handler";
import { assertInstanceOf } from "@std/assert/instance-of";
const handler = new FileHandler("INFO", { filename: "./_tmp/logs.txt" });
handler.setup();
handler.log('Hello, world!');
handler.flush();
handler.destroy();
assertInstanceOf(handler, FileHandler);- msg: string
This handler extends the functionality of the
FileHandlerby "rotating" the log file when it reaches a certain size.maxBytesspecifies the maximum size in bytes that the log file can grow to before rolling over to a new one. If the size of the new log message plus the current log file size exceedsmaxBytesthen a roll-over is triggered. When a roll-over occurs, before the log message is written, the log file is renamed and appended with.1. If a.1version already existed, it would have been renamed.2first and so on. The maximum number of log files to keep is specified bymaxBackupCount. After the renames are complete the log message is written to the original, now blank, file.Example: Given
log.txt,log.txt.1,log.txt.2andlog.txt.3, amaxBackupCountof 3 and a new log message which would causelog.txtto exceedmaxBytes, thenlog.txt.2would be renamed tolog.txt.3(thereby discarding the original contents oflog.txt.3since 3 is the maximum number of backups to keep),log.txt.1would be renamed tolog.txt.2,log.txtwould be renamed tolog.txt.1and finallylog.txtwould be created from scratch where the new log message would be written.This handler uses a buffer for writing log messages to file. Logs can be manually flushed with
fileHandler.flush(). Log messages with a log level greater than ERROR are immediately flushed. Logs are also flushed on process completion.Additional notes on
modeas described above:'a'Default mode. As above, this will pick up where the logs left off in rotation, or create a new log file if it doesn't exist.'w'in addition to starting with a cleanfilename, this mode will also cause any existing backups (up tomaxBackupCount) to be deleted on setup giving a fully clean slate.'x'requires that neitherfilename, nor any backups (up tomaxBackupCount), exist before setup.This handler requires both
--allow-readand--allow-writepermissions on the log files.