-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(logger): docs and runtime #16437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
12635f0
2117d5c
2bfb30e
8d4b558
bb73e45
e6e8dd8
d0d4df7
f57f126
fc7a021
de36899
f40f166
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| --- | ||
| 'astro': minor | ||
| --- | ||
|
|
||
| Adds a new experimental logger, which allows to provide better controls over Astro's logging infrastructure. | ||
|
|
||
| #### JSON logging | ||
|
|
||
| JSON logging can be enabled via CLI or via configuration: | ||
|
|
||
| ```shell | ||
| astro dev --experimentalJson | ||
| astro build --experimentalJson | ||
| astro sync --experimentalJson | ||
| ``` | ||
|
ematipico marked this conversation as resolved.
Outdated
|
||
|
|
||
| ```js | ||
| // astro.config.mjs | ||
| import { defineConfig, logHandlers } from "astro/config"; | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| logger: logHandlers.json({ | ||
| pretty: true, | ||
| level: 'warn' | ||
| }) | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| #### Custom logger | ||
|
|
||
| You can also create your own custom logger by implementing the correct interface: | ||
|
|
||
| ```js | ||
| // astro.config.mjs | ||
| import { defineConfig } from "astro/config"; | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| logger: { | ||
| entrypoint: "@org/custom-logger" | ||
| } | ||
| } | ||
| }) | ||
| ``` | ||
|
|
||
| ```ts | ||
| // @org/custom-logger.js | ||
| import type { AstroLoggerDestination, AstroLoggerMessage } from "astro" | ||
|
|
||
| const customLogger: AstroLoggerDestination = { | ||
| write(message: AstroLoggerMessage) { | ||
| // write message somewhere | ||
| } | ||
| } | ||
| export default customLogger | ||
| ``` | ||
|
ematipico marked this conversation as resolved.
ematipico marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,26 @@ | ||
| import colors from 'piccolore'; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Is this empty comment expected or you forgot to put something here? |
||
| export interface AstroLoggerDestination<T = unknown> { | ||
| /** | ||
| * It receives a message and writes it into a destination | ||
| */ | ||
| write: (chunk: T) => void; | ||
| // NOTE: will document once we actually use it | ||
| /** | ||
| * It dumps logs without closing the connection to the destination. | ||
| * Method that can be used by specialized loggers. | ||
| */ | ||
| flush?: () => Promise<void> | void; | ||
| // NOTE: will document once we actually use it | ||
| /** | ||
| * It dumps logs and closes the connection to the destination. | ||
| * Method that can be used by specialized loggers. | ||
| */ | ||
| close?: () => Promise<void> | void; | ||
| } | ||
|
|
||
| // NOTE: this is a public type | ||
|
|
||
| /** | ||
| * The level of logging. Priority is the following: | ||
| * 1. debug | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,78 @@ | ||
| import type { LoggerHandlerConfig } from './config.js'; | ||
| import type { JonsHandlerConfig } from './impls/json.js'; | ||
| import type { NodeHandlerConfig } from './impls/node.js'; | ||
| import type { ConsoleHandlerConfig } from './impls/console.js'; | ||
|
|
||
| export const logHandlers = { | ||
| /** | ||
| * It uses the built-in Astro JSON logger. | ||
| * @example | ||
| * ```js | ||
| * export default defineConfig({ | ||
| * experimental: { | ||
| * logger: logHandlers.json({ pretty: true }) | ||
| * } | ||
| * }) | ||
| * ``` | ||
| */ | ||
| json(config?: JonsHandlerConfig): LoggerHandlerConfig { | ||
| return { | ||
| entrypoint: 'astro/logger/json', | ||
| config, | ||
| }; | ||
| }, | ||
| node(): LoggerHandlerConfig { | ||
| /** | ||
| * It uses the built-in Astro Node.js logger. | ||
| * | ||
| * @example | ||
| * ```js | ||
| * export default defineConfig({ | ||
| * experimental: { | ||
| * logger: logHandlers.node({ pretty: true }) | ||
| * } | ||
| * }) | ||
| * ``` | ||
| */ | ||
| node(config?: NodeHandlerConfig): LoggerHandlerConfig { | ||
| return { | ||
| entrypoint: 'astro/logger/node', | ||
| config, | ||
| }; | ||
| }, | ||
| console(): LoggerHandlerConfig { | ||
| /** | ||
| * It uses the built-in Astro console logger. | ||
| * | ||
| * @example | ||
| * ```js | ||
| * export default defineConfig({ | ||
| * experimental: { | ||
| * logger: logHandlers.console({ pretty: true }) | ||
| * } | ||
| * }) | ||
| * ``` | ||
| */ | ||
| console(config?: ConsoleHandlerConfig): LoggerHandlerConfig { | ||
| return { | ||
| entrypoint: 'astro/logger/console', | ||
| config, | ||
| }; | ||
| }, | ||
|
|
||
| /** | ||
| * It allows composing different loggers | ||
| * | ||
| * @example | ||
| * ```js | ||
| * export default defineConfig({ | ||
| * experimental: { | ||
| * logger: logHandlers.compose([ | ||
| * logHandlers.console(), | ||
| * logHandlers.json(), | ||
| * ]) | ||
| * } | ||
| * }) | ||
| * ``` | ||
| */ | ||
| compose(...loggers: LoggerHandlerConfig[]): LoggerHandlerConfig { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe neither the documentation nor the changeset mentions this feature. Maybe for the changeset it's not important (ie. we can focus on the most important parts) but we probably want to mention it in the docs.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I left it out in purpose here, I forgot to add to the docs, I will address this shortly |
||
| return { | ||
| entrypoint: 'astro/logger/compose', | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.