This package provides a customizable logging middleware for SvelteKit applications. It allows developers to colorize and format log messages for HTTP requests and responses, facilitating better readability and debugging experience in server-side logs.
The key feature is the ability to define a log message template with dynamic placeholders for request and response details (such as URL, HTTP method, status code, etc.) and colorize the output using predefined colors.
- Customizable log message templates
- Dynamic placeholders for request/response details
- Predefined color functions for text colorization
- Supports bold and regular text styles
- Easy integration with SvelteKit's hook system
To install the package, run the following command in your SvelteKit project directory:
npm i sveltekit-logger-hook
Use the getLoggerHook
function to generate a logging hook and integrate it into your SvelteKit application's hooks configuration:
import { getLoggerHook } from 'sveltekit-logger-hook';
import type { Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
const someHook: Handle = ({ event, resolve }) => resolve(event);
const loggerHook = getLoggerHook({
template: '{date} {url} {method} {status}',
colorOptions: {
date: 'yellow',
url: 'default',
status: ({ status }) => (status >= 400 ? 'redBold' : 'green'),
urlSearchParams: 'default'
}
});
export const handle = sequence(loggerHook, someHook);
Observe colorized and formatted log messages
Start your SvelteKit application, and you should see colorized log messages in your console, formatted according to your template and color settings.
A collection of color functions to format log output. Available color functions are:
default
red
green
yellow
cyan
cyanBold
redBold
greenBold
yellowBold
Generates a logging hook for SvelteKit applications.
-
options
- Configuration for the log message template and colors.template
: A string template for log messages. Use placeholders for dynamic content. Available template variables are{date}
,{method}
,{status}
,{url}
,{urlSearchParams}
.decodeSearchParams
: Optional, if not provided orfalse
is passed,urlSearchParams
will be printed as is, iftrue
it will be passed todecodeURIComponent
before printcolorOptions
: Optional. An object specifying color functions for different parts of the log message. If no value is provided all template parts will have default color. Attributes of this configuration are the same template parts without{}
, and accept values as direct color function names or a function that takes in the current values of the template parts and return a color function name(see thestatus
example)
A SvelteKit handle function that can be used in the application's hooks configuration.
This package is licensed under the MIT License.
Contributions to improve this package are welcome. Please follow the contributing guidelines to submit bugs, feature requests, or pull requests.
For support and questions, please open an issue in the GitHub repository.
This package is not affiliated with or endorsed by Svelte or the SvelteKit project.