Coder Social home page Coder Social logo

sveltekit-logger-hook's Introduction

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}.
    • 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.

sveltekit-logger-hook's People

Contributors

carlos-err406 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.