UNPKG

@devgalena/react-use-logger

Version:

`react-use-logger` is a React hook to log messages. It also contains a function `useLogger` and a class `LoggingContextProvider` to set the logging levels of the library / application

50 lines (49 loc) 1.05 kB
import { useCallback as t, useState as f, useEffect as c } from "react"; import { LoggingLevel as o } from "./types.js"; import { consoleRender as u } from "./console-logger.js"; import { useLogging as v } from "./LoggingContext.js"; function L(r) { const [n, e] = f(() => u); return c(() => { if (r) { const { render: s } = r; e(() => s ?? u); } }, [r]), n; } function g(r) { const [n, e] = f(); return c(() => { if (r) { const { level: s } = r; e(s ?? o.Info); } }, [r]), n; } function b() { const r = v(), n = L(r), e = g(r), s = t( (i) => { e != null && o.Debug >= e && n(o.Debug, i); }, [e, n] ), l = t( (i) => { e != null && o.Info >= e && n(o.Info, i); }, [e, n] ), m = t( (i) => { e != null && o.Warning >= e && n(o.Warning, i); }, [e, n] ), d = t( (i) => { e != null && o.Error >= e && n(o.Error, i); }, [e, n] ); return { debug: s, info: l, warning: m, error: d, level: e }; } export { b as useLogger };