Basic docs for the logging for clients.

main
Zed A. Shaw 2 years ago
parent 208568c662
commit 2192525c7c
  1. 35
      client/logging.js

@ -1,11 +1,19 @@
/*
Really simple logging system in browsers that just makes the usual
`console.log` bindings available in a `log` variable. That makes
the code similar to in the Node.js code such as in `api/`. It also
lets you remove levels as in the `logger()`.
*/
import { log_levels } from "./config.js"; import { log_levels } from "./config.js";
const disabled = () => {}; const disabled = () => {};
/*
We have to do this because rendered pages will include
client components but they don't have a window variable.
We try to use window to bind, and if it doesn't work then
just use the base function. One question? Why bind window?
*/
const bind_logging = () => { const bind_logging = () => {
// We have to do this because rendered pages will include
// client components but they don't have a window variable.
// We try to use window to bind, and if it doesn't work then
// just use the base function. One question? Why bind window?
try { try {
return { return {
debug: console.log.bind(window.console), debug: console.log.bind(window.console),
@ -31,11 +39,28 @@ const all_levels = bind_logging();
const as_entries = Object.entries(all_levels); const as_entries = Object.entries(all_levels);
/*
The main way you create a logger for your code. This will remove
a log level, but it's replaced by this function:
```javascript
const disabled = () => {};
```
___BUG___: I'm not sure if modern compilers will remove this function or not. It
does remove the log message from the output, so if you want to hide debugging infor
spamming the console then that works.
+ `levels Array[string]` -- A list of log levels to display.
*/
export const logger = (levels = log_levels) => { export const logger = (levels = log_levels) => {
return Object.fromEntries(as_entries.map(([level, logf]) => { return Object.fromEntries(as_entries.map(([level, logf]) => {
return levels.includes(level) ? [level, logf] : [level, disabled]; return levels.includes(level) ? [level, logf] : [level, disabled];
})); }));
} }
// this is a default logger that's configured with what's in config.js:log_levels /*
A default logger you can use when you don't care what's being logged
or just want to use the `log_levels` configuration from `client/config.js`.
*/
export const log = logger(); export const log = logger();

Loading…
Cancel
Save