Runtime Logger
Astro provides a logger (typed AstroIntegrationLogger
) configured for each integration on all the official integration hooks. But this logger is only accessible on the code configuring the integration, not for any runtime modules defined by it.
This package allows you to access Astro’s built-in logger from the runtime of a project.
Installing the dependency
npx astro add @inox-tools/runtime-logger
pnpm astro add @inox-tools/runtime-logger
yarn astro add @inox-tools/runtime-logger
For projects
Projects can install the integration included in this package to get a runtime logger for each file in their project:
import runtimeLogger from '@inox-tools/runtime-logger';
export default defineConfig({ integrations: [runtimeLogger()],});
With that integration installed, you can import a logger from @it-astro:logger
from anywhere in your project. The returned logger will be named after the file that imported it.
---import { logger } from '@it-astro:logger';
// Logger will be named 'pages/index.astro'logger.info('Hello World!');---
---import { logger } from '@it-astro:logger';
// Logger will be named 'component/Header.astro'logger.info('Hello World!');---
---import { logger } from '@it-astro:logger';
// Logger will be named 'middleware.ts'logger.info('Hello World!');---
For integrations
Integrations can register their own loggers to be used by the runtime using the runtimeLogger
utility.
The utility takes the parameters of astro:config:setup
and the name of the module where the logger will be made available at runtime:
import { runtimeLogger } from '@inox-tools/runtime-logger';
export default () => ({ name: 'your-integration', hooks: { 'astro:config:setup': (params) => { runtimeLogger(params, { name: 'your-integration', }); }, },});
With that in place, your runtime code can now access the logger by importing the generated module @it-astro:logger:<name>
:
---import { logger } from '@it-astro:logger:your-integration';
logger.info('Hello World!');---
License
Astro Runtime Logger is available under the MIT license.