npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sveltekit-logger-hook

v2.3.0

Published

## Overview

Downloads

55

Readme

README.md

Overview

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.

Features

  • 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

Installation

To install the package, run the following command in your SvelteKit project directory:

npm i sveltekit-logger-hook

Usage

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.

API Reference

Colors

A collection of color functions to format log output. Available color functions are:

  • default
  • red
  • green
  • yellow
  • cyan
  • cyanBold
  • redBold
  • greenBold
  • yellowBold

getLoggerHook(options: LoggerHookOptions): Handle

Generates a logging hook for SvelteKit applications.

Parameters:

  • 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 or false is passed, urlSearchParams will be printed as is, if true it will be passed to decodeURIComponent before print
    • colorOptions: 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 the status example)

Return value:

A SvelteKit handle function that can be used in the application's hooks configuration.

License

This package is licensed under the MIT License.

Contributing

Contributions to improve this package are welcome. Please follow the contributing guidelines to submit bugs, feature requests, or pull requests.

Support

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.