GithubHelp home page GithubHelp logo

dolfbarr / react-log-hook Goto Github PK

View Code? Open in Web Editor NEW
42.0 2.0 2.0 8.04 MB

React hook for logging per component lifecycle

License: MIT License

TypeScript 98.72% JavaScript 1.09% Shell 0.19%
hook logging parcel react react-hook react-lib react-library react-typescript typescript typescript-lib

react-log-hook's Introduction

React Log Hook Screenshot

NPM Version License minzipped size dependency count Build Status Last Commit

๐Ÿชต React Log Hook
Lightweight & customizable logging hook for your react components lifecycle



๐Ÿชต react-log-hook

React hook for logging per component lifecycle

Features

  • ๐Ÿชถ Lightweight โ€” under 1.5 kB gzipped & minified
  • ๐Ÿ—‚๏ธ Typed โ€” made with TypeScript, shipped with types
  • ๐Ÿฅฐ Simple โ€” don't worry about any changes in your props & state
  • ๐Ÿ”ง Customizable โ€” able to change everything you see in the logs
  • ๐Ÿ”ฌ Tested โ€” up to ๐Ÿ’ฏ% unit test coverage
  • ๐ŸŽ๏ธ Fast โ€” native react hooks & optimized
  • ๐Ÿ“ญ No dependecies

Install

With npm

npm install -D react-log-hook

With yarn

yarn add -D react-log-hook

Usage

Basic usage

import { useLog } from 'react-log-hook'

const App = () => {
  // Add a logger
  const { log } = useLog()

  const [state, setState] = useState(null)

  // Log the changes via console in real time!
  log(state)

  return null
}

Configuration options

import { useLog } from 'react-log-hook'

const App = () => {
  // Any configuration properties are optional
  const { log } = useLog({
    environments: [
      /** Contains array of environments of `process.env.NODE_ENV` in which logging will be allowed  */
      'dev',
      'development',
    ],

    // Print Options

    styles: {
      /** Contains styles object with different CSS inline styles used in logging */
      componentCSS:
        'color: DodgerBlue' /** Inline css for rendering component name in the logs */,
      changeCSS:
        'color: green; font-weight: bold;' /** Inline css for rendering current value in the logs */,
      subValueCSS:
        'color: SlateGray; font-weight: thin;' /** Inline css for rendering any additional data like time or previous value in the logs */,
    },
    printer: console /** Contains custom implementation of console */,
    logLevel: 'log' /** Level of logging defined by console method */,
    /** Render object or array inline or via interactive browser renderer */
    inline: true,
    isGroupingEnabled: true /** Enable grouping for logs  */,
    isGroupCollapsed: false /** Render groups collapsed  */,
    groupLabelRenderer: (
      /** A function which will be used to render labels for the group  */
      type /** Current stage of component lifecycle: 'Mount' | 'Change' | 'Unmount'  */,
      componentName,
    ) => `${type}${componentName}`,

    // Custom Render Function

    render: function ({
      /** Custom function which will be used for rendering the result, provided with useful data */
      value,
      prevValue,
      type /** Current stage of component lifecycle: 'Mount' | 'Change' | 'Unmount' */,
      componentName,
      inline /** Render object or array inline or via interactive browser renderer */,
      flags: {
        isGrouped /** Enable grouping for logs  */,
        isCollapsed /** Render groups collapsed  */,
      },
    }) {
      console.log(value)
    },
  })

  const [state, setState] = useState(null)

  // It's possible to redefine any configuration option per log call!
  log(state, {
    inline: false,
    logLevel: 'warn',
  })

  return null
}

FAQ

Will it deep copy the value to make sure it will persist in the logs?

  • ๐ŸŽ‰ Yes, ๐Ÿชต react-log-hook deep copies the value to make sure it will not be changed in the logs later

Do i need to install @types/react-log-hook as well?

  • ๐Ÿ’ช No, ๐Ÿชต react-log-hook comes with prebundled types

Will it run in production evironment?

  • โœ… By default ๐Ÿชต react-log-hook will run only in dev or development node evironments defined by NODE_ENV

Roadmap

  • Add previous state checking
  • Use object copy to persist in time
  • Use console groups to handle all the logs
  • Add dev environment support by default
  • Polish the looks with component names, function calls, time etc
  • Add more customization options
  • Test with SSR & Server components

Contributing

  • ๐ŸŒŸ Stars & ๐Ÿ“ฅ Pull Requests are welcome for sure! โค๏ธ

Development

๐Ÿชต react-log-hook uses npm & npm scripts in development, the following scipts can be handy:

npm run start:demo

Starts a demo app with enabled hook to check it in real environment

npm run storybook

Starts storybook with example components to test against

npm run release:check

Combination of linting, type-checking & tests; runs as precommit hook

License

MIT License

react-log-hook's People

Contributors

dependabot[bot] avatar dolfbarr avatar kenzhemir 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

Watchers

 avatar  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.