Code Monkey home page Code Monkey logo

tinga's Introduction

Tinga

Small browser logging module with the ability to send logs to the remote endpoint

Test Codecov GitHub license

Small (~1KB) logging module primarily for the browser. It has a few logging levels that can be enabled and disabled at runtime.

screen

Install

npm i tinga

Motivation

While pino is an excellent logger for the backend all the functionality is not needed for the frontend, it has a bigger file size, and the default formatting doesn't suit the browser console hence, Tinga has been created.

Usage

Usage is very simple, by default you don't need to pass in any configuration, but I recommend using at least the label property for added visibility in the browser console.

import Tinga from 'tinga'

const logger = new Tinga({ label: 'demo' })
logger.log('Hello world')
logger.error('Wrong username')

Configuration

Tinga accepts a configuration object to customize the Tinga instance.

import Tinga, { type Config } from 'tinga'

const config: Config = {
  ctx: { name: 'Ivan' }, // context
  label: 'demo',
  processData: (ctx, data, _info: { level: Level; label?: string }) => {
    return {
      ctx,
      data
    }
  }
}

const logger = new Tinga(config)
  • ctx: is additional data that will be logged with every log level.
  • label: string to be logged right next to the level.
  • processData: a function that is called just before the data is logged to the console, it should at least return an object with the data property which is an array of data to be passed to the console (as rest arguments).

Context

Context is additional data that can be set per logger instance, that will be passed to every log level. You can get and set context at runtime.

const looger = new Tinga({ ctx: { name: 'ivan' } })

logger.getContext()
logger.setContext({ name: 'Mario' })

Levels

Seven levels can be used for logging.

Name Value
trace 10
debug 20
info 30
warn 40
error 50
critical 60
silent 100

You can get and set levels at runtime, all levels above the currently set level will be logged. silent level is the only level that is not logged, so if you want to disable logging, you can use the silent level.

logger.getLevel()
logger.setLevel('silent')
logger.getLevels() // get all levels

Child instances

You can create a child instance of an already created Tinga instance, by creating a child instance, it will inherit all the configuration properties of the parent. Child instances are useful when you want to slightly modify the original version, by for example having a different label, for modifying the parent context. For a child instance, you can directly set the new context, or by passing a function as the context property you can derive a new context from the parent context, as the example below shows.

const logger = new Tinga(config)

const childLogger = logger.child({
  label: 'new-labels',
  ctx: (parentCtx) => {
    return {
      ...parentCtx,
      newProps: 'hello from child'
    }
  }
})

childLogger.log('helo')

Examples

Check out this Next.js logging demo where I'm using (comparing) Pino and Tinga on the same page.

tinga's People

Contributors

github-actions[bot] avatar ivandotv avatar renovate-bot avatar renovate[bot] avatar simenandre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tinga's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/CI.yml
  • actions/checkout v4
  • actions/setup-node v4
  • pnpm/action-setup v3.0.0
  • actions/cache v3
  • changesets/action v1
npm
package.json
  • @biomejs/biome 1.5.3
  • @changesets/cli ^2.27.1
  • @types/node ^20.11.24
  • @vitest/coverage-v8 ^1.3.1
  • jsdom ^24.0.0
  • lefthook ^1.6.5
  • microbundle ^0.15.1
  • shx ^0.3.4
  • typedoc ^0.25.11
  • typedoc-plugin-markdown ^3.17.1
  • typescript ^5.3.3
  • vitest ^1.3.1

  • Check this box to trigger a request for Renovate to run again on this repository

Send logs asynchronous (bulk logs)

Hello ๐Ÿ‘‹

Thank you for this package.

I am building a Fastify plugin for receiving logs from the frontend (and passing them to a pino instance). This is hopefully going to help us pass logs more easily from our frontend applications. The plugin is made so multiple logs can be sent (basically an array of log objects). I'm thinking about using tinga as the frontend counterpart. I would love to see a way to send bulk of logs, instead of one request per log event.

I would love to create a PR for it :)

Freeze configuration object

Since some properties of the configuration object can be accessed by the outside code, in order to disable modification,
every time the code is accessed new object should be returned or the initial configuration object should be frozen.

If object is frozen, then when setting the configuration from outside (setContext(), setLevel(), setRemoteLevel()) configuration object need to be copied, changed and frozen again.

All this will increase module size.

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.