Code Monkey home page Code Monkey logo

zoya's Introduction

Truly highly composable logging utility written in TypeScript

Header

Build Status Coverage Status NPM Downloads

Description

Zoya is a highly composable logging library used for both client and server applications.

Visit the contributing guidelines to learn more on how to translate this document into more languages.

Come over to Discord or Twitter to share your thoughts on the project.

This is a complete rewrite of Signale, written by Klaus Siani, and it's NOT intended to be a drop-in replacement.

Highlights

  • Easy to setup and compose as you like
  • Easy to control how things look
  • Easy to extend
  • Clean and beautiful output, configure as you wish
  • Supports JSON outputs
  • Extensible fields with several fields built in
    • Labels, Badge (emoji), Scopes, Level, Separators, Message, Context
  • Multiple configurable writable streams with stream level filtering
  • Simple and minimal syntax
  • Written in TypeScript

Contents

Install

Yarn

yarn add zoya

NPM

npm install zoya

Usage

Overview

Zoya tries to make it simple to configure and compose your loggers, so you can easily control how the output will look like. This is possible due to the field chain passed to the logger in its creation.

To use the default exported logger you just import and use it.

import zoya from "zoya";

zoya.info("Hello world!");

To see all the available examples check the examples folder

Default logger

The default export of zoya is an instance of a logger (called the default logger). This instance is preconfigured to write to process.stdout with all logging levels enabled (so you can use debug and trace for example).

These are all the available loggers in the default logger.

import log from "zoya";

log.trace("trace messages...");
log.debug("debug messages...");
log.info("info messages...");
log.success("success messages...");
log.warn("warn messages...");
log.error("error messages...");
log.failed("failed messages...");
log.fatal("fatal messages...");

The fields configured in the default logger are:

[scope][separator][badge][label][level][message][context]

More on fields later.

On new instances

To create a new Zoya instance, use zoya function passing the proper configs you want. Note that all first level values uses the default values from the default logger, so you don't need to specify all of them if you want for example to just enable json logging.

Note that if you pass value to an object, for example types, all the child values are overwritten.

import { zoya, Level } from "zoya";

// Just enables json
const jsonLogger = zoya({ json: true });

// Only outputs to stderr
const stderrLogger = zoya({
  streams: [
    {
      level: Level.error,
      stream: process.stderr
    }
  ]
});

Enhancing loggers

Zoya allows you to enhance existing loggers in order to add new logging types. Enhancing logger will create a new logger instance and will keep the old logger intact.

import { bold, underline } from "chalk";
import log, { Level } from "zoya";

const xmasLogger = log.enhance({
  santa: {
    level: Level.info,
    options: {
      badge: "santa"
      label: {
        name: "santa",
        transformer: label => bold(underline(label))
      }
      scope: ["xmas"]
    }
  }
});

xmasLogger.santa("Hohoho!");

The above logger will output something like this

[xmas] ยป ๐ŸŽ…  santa Hohoho!

Configuration

Fields

Zoya has the concept of fields. All log messages are built by a chain of fields that are configured when creating a new logger through zoya function. Fields are context sensitive, this means that you can output different things in a field depending on what the message contains.

Configuring fields

For example, if you just want to show the message and its context, you can configure zoya like this:

import { context, message, zoya } from "zoya";

const custom = zoya({
  fields: [message(), context()]
});

custom.info("Hello world", { ip: "127.0.0.1" });

This will output something like this in text mode:

Hello world {
  "ip": "127.0.0.1"
}

And this in JSON mode:

{"message":"Hello world","context":{"ip":"127.0.0.1"}}

Zoya fields

Zoya contains several fields that can be imported from zoya package.

Badge

Used to display emojis in the log text

Context

Displays the message context

Label

Displays a customizable label for each message type

Level

Adds the message level only on json outputs

Message

Displays the message itself

Scope

Display the message scope(s)

Separator

Displays a customizable separator only in text mode


Custom fields

You can easily write custom fields.

TODO: write example custom fields. For now you can take the bundled ones as an example from here.

Development

For more info on how to contribute to the project, please read the contributing guidelines.

  • Fork the repository and clone it to your machine
  • Navigate to your local fork: cd zoya
  • Install the project dependencies: npm install or yarn install
  • Lint code for errors: npm test or yarn test

Related

  • Signale - Highly configurable logging utility

Team

FAQ

Why "Zoya"?

Because I like Trine :)

Acknowledgements

Thanks

Huge thanks to brain (brain#4221), Micah (Micha#6878) and undefined.toString() (elderapo#8225) for the helpful insights over TypeScript discord channel

License

MIT

zoya's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar gluons avatar wolfulus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

zoya's Issues

Messages not aligned, emoji not visible

Describe the bug
The default log messages are not correctly aligned in the terminal.
Also the emoji for the warning is not correctly displayed.

Information about your versions
Zoya: 1.0.1
Node: 14.8.0
npm: 6.14.7
VSCode: 1.48.0

I am using the Linux sub system for Windows (WSL) with Ubuntu 20.04.
I swapped the integrated terminal in my VSCode to the WSL terminal.
Currently using the ZSH shell with Oh-My-ZSH and the powerlevel10k theme.

Furhter information
Here is a screenshot of my code and how it looks in the console.

As you can see the last emoji is not visible correctly and the lines have different
starting points and are not aligned equally.

Pretty Printing JSON in text mode

Hello!

My team and I are working to integrate zoya into our project.

We'd like to use JSON logs in prod (which I understand zoya has the capacity to do quite easily).
However, when we do local development in VSCode, we'd like to output to text mode.

The problem is that in text mode, objects are printed as [zoya] ยป ๐Ÿ”ต info [object Object]

Is there any way to resolve this without having to manually encapsulate every instance of a logged object with JSON.stringify?

Add support for using this in the browser

Is your feature request related to a problem? Please describe.
It would be wonderful if I could rely on the same great logging tool both for my front- and back-end.

Describe the solution you'd like
Make it work in the browser as well

Additional context
n/a

Unable to resolve module 'util'

I tried to use zoya in React Native. I got the error:

Simulator Screen Shot - iPhone 11 - 2019-12-20 at 13 09 24

Do you know any suggestion for that? I really want to use zoya from my mobile projects :)

Add `success` type

Is your feature request related to a problem? Please describe.
I want to print some success message but zoya don't have success.

log.trace("Hello from zoya!");
log.debug("Hello from zoya!");
log.info("Hello from zoya!");
log.warn("Hello from zoya!");
log.error("Hello from zoya!");
log.fatal("Hello from zoya!");

Describe the solution you'd like
Please add success type.

Additional context
-

Disable Emoji option

Is your feature request related to a problem? Please describe.
Most windows setups have difficulties with emojis, this can be dependent on the TTY, PTY or font used. For example it works in the new Windows terminal (preview) and Powershell Core(6.) but not from CMD, VSCode or VisualStudio with Powershell Core(6.).

image

Describe the solution you'd like
A simple option to disable/enable emojis at one place. The developers can decide if they want to toggle it for their environment.

Add some docs about how to use `scope`

Hey, thanks for this little gem of a library ๐Ÿ‘ ๐ŸŽ‰

I was trying to figure out how to replace the default zoya scope in the log messages, and here's what I came up with:

const logger = zoya({
  fields: [scope({ scopes: ['main'] }), separator(), badge(), label(), separator(), message(), context()],
})

Is this the right way to do it? I figured this out by reading the tests (which were very easy to read ๐Ÿ‘ ) but I'm not sure if I've missed something:

  • why is scopes an array? What happens if I define multiple scopes?
  • can I specify the scope when logging a message? If so, how?

I guess some more examples might be useful. The santa one is cute but not very illustrative :)

If you prefer to just explain to me here in the issue how to use it, I can send a PR with an update to the docs.

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.