Skip to content

Commit

Permalink
Merge branch '0.5-maintenance'
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Sep 15, 2024
2 parents ef685f2 + 8df7cf9 commit 8e38956
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 78 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ jobs:
set -ex
bun install
if [[ "$GITHUB_REF_TYPE" = "tag" ]]; then
bun add -D "@logtape/logtape@$GITHUB_REF_NAME"
bun add -D @logtape/otel@latest
EXTRA_NAV_TEXT=Unstable \
EXTRA_NAV_LINK="$UNSTABLE_DOCS_URL" \
bun run build
else
bun add -D @logtape/logtape@dev
bun add -D @logtape/otel@dev
EXTRA_NAV_TEXT=Stable \
EXTRA_NAV_LINK="$STABLE_DOCS_URL" \
bun run build
Expand Down
16 changes: 16 additions & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Presets, SingleBar } from "cli-progress";
import { jsrRef } from "markdown-it-jsr-ref";
import { defineConfig } from "vitepress";
import { transformerTwoslash } from "@shikijs/vitepress-twoslash";

const progress = new SingleBar({}, Presets.shades_classic);
let started = false;
Expand Down Expand Up @@ -110,6 +111,21 @@ export default defineConfig({
},
head: plausibleScript,
markdown: {
codeTransformers: [
transformerTwoslash({
twoslashOptions: {
compilerOptions: {
lib: ["dom", "dom.iterable", "esnext"],
types: [
"dom",
"dom.iterable",
"esnext",
"@teidesu/deno-types/full",
],
},
},
}),
],
config(md) {
md.use(jsrRefPlugin);
},
Expand Down
13 changes: 11 additions & 2 deletions docs/.vitepress/theme/index.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import DefaultTheme from 'vitepress/theme'
import TwoslashFloatingVue from "@shikijs/vitepress-twoslash/client";
import type { EnhanceAppContext } from "vitepress";
import Theme from "vitepress/theme";

import "@shikijs/vitepress-twoslash/style.css";
import './custom.css'

export default DefaultTheme
export default {
extends: Theme,
enhanceApp({ app }: EnhanceAppContext) {
app.use(TwoslashFloatingVue);
},
};
Binary file modified docs/bun.lockb
Binary file not shown.
22 changes: 18 additions & 4 deletions docs/manual/categories.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ the log level of loggers at different levels of the category hierarchy.

Here's an example of setting log levels for different categories:

~~~~ typescript{9-10}
~~~~ typescript{9-10} twoslash
import { configure, getConsoleSink } from "@logtape/logtape";
await configure({
Expand All @@ -35,15 +35,19 @@ Child loggers

You can get a child logger from a parent logger by calling `~Logger.getChild()`:

~~~~ typescript
~~~~ typescript twoslash
import { getLogger } from "@logtape/logtape";
// ---cut-before---
const logger = getLogger(["my-app"]);
const childLogger = logger.getChild("my-module");
// equivalent: const childLogger = getLogger(["my-app", "my-module"]);
~~~~

The `~Logger.getChild()` method can take an array of strings as well:

~~~~ typescript
~~~~ typescript twoslash
import { getLogger } from "@logtape/logtape";
// ---cut-before---
const logger = getLogger(["my-app"]);
const childLogger = logger.getChild(["my-module", "foo"]);
// equivalent: const childLogger = getLogger(["my-app", "my-module", "foo"]);
Expand Down Expand Up @@ -75,7 +79,17 @@ to an empty array.
> if there's an issue with your main sink, you can still receive meta logs about
> the issue:
>
> ~~~~ typescript
> ~~~~ typescript twoslash
> // @noErrors: 2307
> import { type Sink } from "@logtape/logtape";
> /**
> * A hypothetical function to get your main sink.
> * @returns The main sink.
> */
> function getYourMainSink(): Sink {
> return 0 as unknown as Sink;
> }
> // ---cut-before---
> import { configure, getConsoleSink } from "@logtape/logtape";
> import { getYourMainSink } from "./your-main-sink.ts";
>
Expand Down
39 changes: 30 additions & 9 deletions docs/manual/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ At its core, configuring LogTape involves three main components:

Here's a simple configuration to get you started:

~~~~ typescript
~~~~ typescript twoslash
import { configure, getConsoleSink } from "@logtape/logtape";

await configure({
Expand Down Expand Up @@ -53,7 +53,8 @@ Crafting your configuration
[Sinks](./sinks.md) determine where your logs end up. You can have multiple
sinks for different purposes:

~~~~ typescript
~~~~ typescript twoslash
// @noErrors: 2345
import { configure, getConsoleSink, getFileSink } from "@logtape/logtape";

await configure({
Expand All @@ -71,7 +72,10 @@ await configure({
[Filters](./filters.md) allow you to fine-tune which logs are processed. They
can be based on log levels, content, or custom logic:

~~~~ typescript
~~~~ typescript twoslash
// @noErrors: 2345
import { configure } from "@logtape/logtape";
// ---cut-before---
await configure({
// ... sinks configuration
filters: {
Expand All @@ -82,7 +86,9 @@ await configure({
return record.level === "error" || record.level === "fatal";
},
containsUserData(record) {
return record.message.some(part => part.includes("user"));
return record.message.some(
part => typeof part === "string" && part.includes("user")
);
},
},
// ... loggers configuration
Expand All @@ -94,7 +100,10 @@ await configure({
Loggers are where you bring everything together. You can set up different
loggers for different parts of your application:

~~~~ typescript
~~~~ typescript twoslash
// @noErrors: 2345
import { configure } from "@logtape/logtape";
// ---cut-before---
await configure({
// ... sinks and filters configuration
loggers: [
Expand Down Expand Up @@ -139,7 +148,9 @@ Here's how you might do that:

::: code-group

~~~~ typescript{1,6,11-12} [Deno]
~~~~ typescript{1,6,11-12} twoslash [Deno]
import { configure, getConsoleSink, getFileSink } from "@logtape/logtape";
// ---cut-before---
const isDevelopment = Deno.env.get("DENO_DEPLOYMENT_ID") == null;
await configure({
Expand All @@ -157,7 +168,11 @@ await configure({
});
~~~~

~~~~ typescript{1,6,11-12} [Node]
~~~~ typescript{1,6,11-12} twoslash [Node.js]
import "@types/node";
import process from "node:process";
import { configure, getConsoleSink, getFileSink } from "@logtape/logtape";
// ---cut-before---
const isDevelopment = process.env.NODE_ENV === "development";
await configure({
Expand All @@ -183,7 +198,10 @@ Remember that calling `configure()` with `reset: true` option will reset any
existing configuration. If you need to change the configuration at runtime,
you can call `configure()` again with `reset: true` and the new settings:

~~~~ typescript
~~~~ typescript twoslash
import { type Config, configure } from "@logtape/logtape";
const initialConfig = {} as unknown as Config<string, string>;
// ---cut-before---
// Initial configuration
await configure(initialConfig);

Expand All @@ -204,7 +222,10 @@ await configure({

Or you can explicitly call `reset()` to clear the existing configuration:

~~~~ typescript
~~~~ typescript twoslash
import { type Config } from "@logtape/logtape";
const initialConfig = {} as unknown as Config<string, string>;
// ---cut-before---
import { configure, reset } from "@logtape/logtape";

await configure(initialConfig);
Expand Down
10 changes: 7 additions & 3 deletions docs/manual/contexts.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ messages. A context is a key-value map. You can set a context for a logger
and log messages `~Logger.with()` the context. Here's an example of setting
a context for a logger:

~~~~ typescript
~~~~ typescript twoslash
import { getLogger } from "@logtape/logtape";
// ---cut-before---
const logger = getLogger(["my-app", "my-module"]);
const ctx = logger.with({ userId: 1234, requestId: "abc" });
ctx.info `This log message will have the context (userId & requestId).`;
Expand All @@ -18,10 +20,12 @@ ctx.warn("Context can be used inside message template: {userId}, {requestId}.");
The context is inherited by child loggers. Here's an example of setting a
context for a parent logger and logging messages with a child logger:

~~~~ typescript
~~~~ typescript twoslash
import { getLogger } from "@logtape/logtape";
// ---cut-before---
const logger = getLogger(["my-app"]);
const parentCtx = logger.with({ userId: 1234, requestId: "abc" });
const childCtx = parentCtx.getLogger(["my-module"]);
const childCtx = parentCtx.getChild(["my-module"]);
childCtx.debug("This log message will have the context: {userId} {requestId}.");
~~~~

Expand Down
27 changes: 18 additions & 9 deletions docs/manual/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ and returns a boolean value. If the filter returns `true`, the log record is
passed to the sinks; otherwise, the log record is discarded. The signature of
`Filter` is:

~~~~ typescript
~~~~ typescript twoslash
import type { LogRecord } from "@logtape/logtape";
// ---cut-before---
export type Filter = (record: LogRecord) => boolean;
~~~~

Expand All @@ -17,14 +19,17 @@ the `~Config.loggers` object to assign filters to loggers.
For example, the following filter discards log messages whose property `elapsed`
is less than 100 milliseconds:

~~~~ typescript {5-9}
~~~~ typescript{5-9} twoslash
// @noErrors: 2345
import { configure, type LogRecord } from "@logtape/logtape";
await configure({
// Omitted for brevity
filters: {
tooSlow(record: LogRecord) {
return "elapsed" in record.properties && record.properties.elapsed >= 100;
return "elapsed" in record.properties
&& typeof record.properties.elapsed === "number"
&& record.properties.elapsed >= 100;
},
},
loggers: [
Expand All @@ -47,12 +52,13 @@ filter log messages by their log levels. The level filter factory takes
a `LogLevel` string and returns a level filter. For example, the following
level filter discards log messages whose log level is less than `info`:

~~~~ typescript
import { getLevelFilter } from "@logtape/logtape";
~~~~ typescript twoslash
// @noErrors: 2345
import { configure, getLevelFilter } from "@logtape/logtape";

await configure({
filters: {
infoOrHigher: getLevelFilter("info"); // [!code highlight]
infoOrHigher: getLevelFilter("info"), // [!code highlight]
},
// Omitted for brevity
});
Expand All @@ -65,14 +71,17 @@ Sink filter
A sink filter is a filter that is applied to a specific [sink](./sinks.md).
You can add a sink filter to a sink by decorating the sink with `withFilter()`:

~~~~ typescript {5-8}
import { getConsoleSink, withFilter } from "@logtape/logtape";
~~~~ typescript{5-8} twoslash
// @noErrors: 2345
import { configure, getConsoleSink, withFilter } from "@logtape/logtape";
await configure({
sinks: {
filteredConsole: withFilter(
getConsoleSink(),
log => "elapsed" in log.properties && log.properties.elapsed >= 100,
log => "elapsed" in log.properties &&
typeof log.properties.elapsed === "number" &&
log.properties.elapsed >= 100,
),
},
// Omitted for brevity
Expand Down
8 changes: 6 additions & 2 deletions docs/manual/formatters.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,9 @@ Fully customized text formatter
A text formatter is just a function that takes a log record and returns
a string. The type of a text formatter is `TextFormatter`:

~~~~ typescript
~~~~ typescript twoslash
import type { LogRecord } from "@logtape/logtape";
// ---cut-before---
export type TextFormatter = (record: LogRecord) => string;
~~~~

Expand All @@ -250,7 +252,9 @@ a function that takes a log record and returns a string. For example,
the following function is a text formatter that formats log records into
[JSON Lines]:

~~~~ typescript
~~~~ typescript twoslash
import type { LogRecord } from "@logtape/logtape";
// ---cut-before---
function jsonLinesFormatter(record: LogRecord): string {
return JSON.stringify(record) + "\n";
}
Expand Down
Loading

0 comments on commit 8e38956

Please sign in to comment.