Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Apr 16, 2024
0 parents commit b48a4e2
Show file tree
Hide file tree
Showing 20 changed files with 2,073 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage/
6 changes: 6 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"recommendations": [
"denoland.vscode-deno",
"streetsidesoftware.code-spell-checker"
]
}
29 changes: 29 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"deno.enable": true,
"deno.unstable": true,
"files.eol": "\n",
"files.insertFinalNewline": true,
"files.trimFinalNewlines": true,
"[json]": {
"editor.defaultFormatter": "vscode.json-language-features",
"editor.formatOnSave": true
},
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features",
"editor.formatOnSave": true
},
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
},
"cSpell.words": [
"consolemock",
"deno",
"hongminhee",
"logtape",
"supercategory"
]
}
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright 2024 Hong Minhee

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- deno-fmt-ignore-file -->

LogTape
=======

LogTape is a simple logging library for Deno/Node.js/Bun/browsers. It is
designed to be used for both applications and libraries.

Currently, LogTape provides only few sinks, but you can easily add your own
sinks.
20 changes: 20 additions & 0 deletions deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@hongminhee/logtape",
"version": "0.1.0",
"exports": "./logtape/mod.ts",
"imports": {
"@std/assert": "jsr:@std/assert@^0.222.1",
"@std/async": "jsr:@std/async@^0.222.1",
"@std/testing": "jsr:@std/testing@^0.222.1",
"consolemock": "npm:consolemock@^1.1.0"
},
"lock": false,
"tasks": {
"check": "deno check **/*.ts && deno lint",
"test": "deno test --allow-read=logtape/__snapshots__ --allow-write=logtape/__snapshots__",
"coverage": "rm -rf coverage && deno task test --coverage && deno coverage --html coverage",
"hooks:install": "deno run --allow-read=deno.json,.git/hooks/ --allow-write=.git/hooks/ jsr:@hongminhee/deno-task-hooks",
"hooks:pre-commit": "deno task check",
"hooks:pre-push": "deno task test"
}
}
70 changes: 70 additions & 0 deletions logtape/__snapshots__/sink.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
export const snapshot = {};

snapshot[`getStreamSink() 1`] = `
"2023-11-14 22:13:20.000 +00:00 [DBG] Hello, 123 & 456!
2023-11-14 22:13:20.000 +00:00 [INF] Hello, 123 & 456!
2023-11-14 22:13:20.000 +00:00 [WRN] Hello, 123 & 456!
2023-11-14 22:13:20.000 +00:00 [ERR] Hello, 123 & 456!
2023-11-14 22:13:20.000 +00:00 [FTL] Hello, 123 & 456!
"
`;

snapshot[`getConsoleSink() 1`] = `
[
{
DEBUG: [
"%cDEBUG%c %cmy-app·junk %cHello, %o & %o!",
"background-color: gray; color: white;",
"background-color: default;",
"color: gray;",
"color: default;",
123,
456,
],
},
{
INFO: [
"%cINFO%c %cmy-app·junk %cHello, %o & %o!",
"background-color: white; color: black;",
"background-color: default;",
"color: gray;",
"color: default;",
123,
456,
],
},
{
WARN: [
"%cWARNING%c %cmy-app·junk %cHello, %o & %o!",
"background-color: orange;",
"background-color: default;",
"color: gray;",
"color: default;",
123,
456,
],
},
{
ERROR: [
"%cERROR%c %cmy-app·junk %cHello, %o & %o!",
"background-color: red;",
"background-color: default;",
"color: gray;",
"color: default;",
123,
456,
],
},
{
ERROR: [
"%cFATAL%c %cmy-app·junk %cHello, %o & %o!",
"background-color: maroon;",
"background-color: default;",
"color: gray;",
"color: default;",
123,
456,
],
},
]
`;
145 changes: 145 additions & 0 deletions logtape/config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { assertEquals, assertThrows } from "@std/assert";
import { ConfigError, configure, reset } from "./config.ts";
import type { Filter } from "./filter.ts";
import { LoggerImpl } from "./logger.ts";
import type { LogRecord } from "./record.ts";
import type { Sink } from "./sink.ts";

Deno.test("configure()", async (t) => {
await t.step("test", () => {
const a: Sink = () => {};
const b: Sink = () => {};
const cLogs: LogRecord[] = [];
const c: Sink = cLogs.push.bind(cLogs);
const x: Filter = () => true;
const y: Filter = () => true;
configure({
sinks: { a, b, c },
filters: { x, y },
loggers: [
{
category: "my-app",
sinks: ["a"],
filters: ["x"],
},
{
category: ["my-app", "foo"],
sinks: ["b"],
filters: ["y"],
},
{
category: ["my-app", "bar"],
sinks: ["c"],
level: "info",
},
],
});

const logger = LoggerImpl.getLogger("my-app");
assertEquals(logger.sinks, [a]);
assertEquals(logger.filters, [x]);
const foo = LoggerImpl.getLogger(["my-app", "foo"]);
assertEquals(foo.sinks, [b]);
assertEquals(foo.filters, [y]);
const bar = LoggerImpl.getLogger(["my-app", "bar"]);
assertEquals(bar.sinks, [c]);
bar.debug("ignored");
assertEquals(cLogs, []);
bar.info("logged");
assertEquals(cLogs, [
{
level: "info",
category: ["my-app", "bar"],
message: ["logged"],
properties: {},
timestamp: cLogs[0].timestamp,
},
]);
});

await t.step("reconfigure", () => {
assertThrows(
() =>
configure({
sinks: {},
filters: {},
loggers: [{ category: "my-app" }],
}),
ConfigError,
"Already configured",
);

// No exception if reset is true:
configure({
sinks: {},
filters: {},
loggers: [{ category: "my-app" }],
reset: true,
});
});

await t.step("tear down", () => {
reset();
});

await t.step("misconfiguration", () => {
assertThrows(
() =>
configure({
// deno-lint-ignore no-explicit-any
sinks: {} as any,
filters: {},
loggers: [
{
category: "my-app",
sinks: ["invalid"],
},
],
reset: true,
}),
ConfigError,
"Sink not found: invalid",
);

assertThrows(
() =>
configure({
sinks: {},
// deno-lint-ignore no-explicit-any
filters: {} as any,
loggers: [
{
category: "my-app",
filters: ["invalid"],
},
],
reset: true,
}),
ConfigError,
"Filter not found: invalid",
);
});

const metaCategories = [[], ["logtape"], ["logtape", "meta"]];
for (const metaCategory of metaCategories) {
await t.step("meta configuration: " + JSON.stringify(metaCategory), () => {
configure({
sinks: {},
filters: {},
loggers: [
{
category: metaCategory,
sinks: [],
filters: [],
},
],
});

assertEquals(LoggerImpl.getLogger(["logger", "meta"]).sinks, []);
});

await t.step("tear down", () => {
reset();
});
}
});
Loading

0 comments on commit b48a4e2

Please sign in to comment.