2727React hook for logging per component lifecycle
2828
2929## Features
30- - 🪶 ** Lightweight** — under * 2 kB* gzipped
31- - 🗂️ ** Typed** — made with TypeScript, ships with types
30+ - 🪶 ** Lightweight** — under * 1.5 kB* gzipped & minified
31+ - 🗂️ ** Typed** — made with TypeScript, shipped with types
3232- 🥰 ** Simple** — don't worry about any changes in your props & state
33- - 🔧 ** Customizable** — work in progress 😉
34- - 🔬 ** Tested** — up to 100% coverage
33+ - 🔧 ** Customizable** — able to change everything you see in the logs
34+ - 🔬 ** Tested** — 💯% test coverage
3535- 🏎️ ** Fast** — native react hooks & optimized
3636- 📭 ** No dependecies**
3737
@@ -53,6 +53,8 @@ yarn add -D react-log-hook
5353
5454## Usage
5555
56+ ### Basic usage
57+
5658``` javascript
5759import {useLog } from ' react-log-hook'
5860
@@ -69,6 +71,74 @@ const App = () => {
6971}
7072```
7173
74+ ### Configuration options
75+
76+ ``` javascript
77+ import {useLog } from ' react-log-hook'
78+
79+ const App = () => {
80+ // Any configuration properties are optional
81+ const { log } = useLog ({
82+ environments: [
83+ /** Contains array of environments of `process.env.NODE_ENV` in which logging will be allowed */
84+ ' dev' ,
85+ ' development' ,
86+ ],
87+
88+ // Print Options
89+
90+ styles: {
91+ /** Contains styles object with different CSS inline styles used in logging */
92+
93+ componentCSS:
94+ ' color: DodgerBlue' /** Inline css for rendering component name in the logs */ ,
95+ changeCSS:
96+ ' color: green; font-weight: bold;' /** Inline css for rendering current value in the logs */ ,
97+ subValueCSS:
98+ ' color: SlateGray; font-weight: thin;' /** Inline css for rendering any additional data like time or previous value in the logs */ ,
99+ },
100+ printer: console /** Contains custom implementation of console */ ,
101+ logLevel: ' log' /** Level of logging defined by console method */ ,
102+ /** Render object or array inline or via interactive browser renderer */
103+ inline: true ,
104+ isGroupingEnabled: true /** Enable grouping for logs */ ,
105+ isGroupCollapsed: false /** Render groups collapsed */ ,
106+ groupLabelRenderer: (
107+ /** A function which will be used to render labels for the group */
108+ type /** Current stage of component lifecycle: 'Mount' | 'Change' | 'Unmount' */ ,
109+ componentName,
110+ ) => ` ${ type}${ componentName} ` ,
111+
112+ // Custom Render Function
113+
114+ render : function ({
115+ /** Custom function which will be used for rendering the result, provided with useful data */
116+ value,
117+ prevValue,
118+ type /** Current stage of component lifecycle: 'Mount' | 'Change' | 'Unmount' */ ,
119+ componentName,
120+ inline /** Render object or array inline or via interactive browser renderer */ ,
121+ flags: {
122+ isGrouped /** Enable grouping for logs */ ,
123+ isCollapsed /** Render groups collapsed */ ,
124+ },
125+ }): void {
126+ console .log (value)
127+ },
128+ })
129+
130+ const [state , setState ] = useState (null )
131+
132+ // It's possible to redefine any configuration option per log call!
133+ log (state, {
134+ inline: false ,
135+ logLevel: ' warn'
136+ })
137+
138+ return null
139+ }
140+ ```
141+
72142## FAQ
73143
74144### Will it deep copy the value to make sure it will persist in the logs?
0 commit comments