forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve logging module (denoland#51)
- Loading branch information
1 parent
77831c3
commit 439885c
Showing
9 changed files
with
276 additions
and
170 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,38 @@ | ||
# Logging module for Deno | ||
# Basic usage | ||
|
||
Very much work in progress. Contributions welcome. | ||
```ts | ||
import * as log from "https://deno.land/x/std/logging/index.ts"; | ||
|
||
This library is heavily inspired by Python's | ||
[logging](https://docs.python.org/3/library/logging.html#logging.Logger.log) | ||
module, altough it's not planned to be a direct port. Having separate loggers, | ||
handlers, formatters and filters gives developer very granular control over | ||
logging which is most desirable for server side software. | ||
// simple console logger | ||
log.debug("Hello world"); | ||
log.info("Hello world"); | ||
log.warning("Hello world"); | ||
log.error("Hello world"); | ||
log.critical("500 Internal server error"); | ||
|
||
Todo: | ||
// configure as needed | ||
await log.setup({ | ||
handlers: { | ||
console: new log.handlers.ConsoleHandler("DEBUG"), | ||
file: new log.handlers.FileHandler("WARNING", "./log.txt"), | ||
}, | ||
|
||
- [ ] implement formatters | ||
- [ ] implement `FileHandler` | ||
- [ ] tests | ||
loggers: { | ||
default: { | ||
level: "DEBUG", | ||
handlers: ["console", "file"], | ||
} | ||
} | ||
}); | ||
|
||
// get configured logger | ||
const logger = log.getLogger("default"); | ||
logger.debug("fizz") // <- logs to `console`, because `file` handler requires 'WARNING' level | ||
logger.warning("buzz") // <- logs to both `console` and `file` handlers | ||
|
||
// if you try to use a logger that hasn't been configured | ||
// you're good to go, it gets created automatically with level set to 0 | ||
// so no message is logged | ||
const unknownLogger = log.getLogger("mystery"); | ||
unknownLogger.info("foobar") // no-op | ||
``` |
This file was deleted.
Oops, something went wrong.
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,65 @@ | ||
import { open, File, Writer } from "deno"; | ||
import { getLevelByName } from "./levels.ts"; | ||
import { LogRecord } from "./logger.ts"; | ||
|
||
export class BaseHandler { | ||
level: number; | ||
levelName: string; | ||
|
||
constructor(levelName: string) { | ||
this.level = getLevelByName(levelName); | ||
this.levelName = levelName; | ||
} | ||
|
||
handle(logRecord: LogRecord) { | ||
if (this.level > logRecord.level) return; | ||
|
||
// TODO: implement formatter | ||
const msg = `${logRecord.levelName} ${logRecord.msg}`; | ||
|
||
return this.log(msg); | ||
} | ||
|
||
log(msg: string) { } | ||
async setup() { } | ||
async destroy() { } | ||
} | ||
|
||
|
||
export class ConsoleHandler extends BaseHandler { | ||
log(msg: string) { | ||
console.log(msg); | ||
} | ||
} | ||
|
||
|
||
export abstract class WriterHandler extends BaseHandler { | ||
protected _writer: Writer; | ||
|
||
log(msg: string) { | ||
const encoder = new TextEncoder(); | ||
// promise is intentionally not awaited | ||
this._writer.write(encoder.encode(msg + "\n")); | ||
} | ||
} | ||
|
||
|
||
export class FileHandler extends WriterHandler { | ||
private _file: File; | ||
private _filename: string; | ||
|
||
constructor(levelName: string, filename: string) { | ||
super(levelName); | ||
this._filename = filename; | ||
} | ||
|
||
async setup() { | ||
// open file in append mode - write only | ||
this._file = await open(this._filename, 'a'); | ||
this._writer = this._file; | ||
} | ||
|
||
async destroy() { | ||
await this._file.close(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.