appy-one / acebase

A fast, low memory, transactional, index & query enabled NoSQL database engine and server for node.js and browser with realtime data change notifications
MIT License
487 stars 27 forks source link

Logging API support? #134

Open SilentAntenna opened 2 years ago

SilentAntenna commented 2 years ago

It would be nice if Acebase could provide a logging callback or a logging event. In this way, we may use libraries like log4js to save the logs for future inspection.

appy-one commented 2 years ago

Thanks for your input, I'll see what I can do! 👍🏼

Azarattum commented 1 year ago

I agree that it would be extremely useful. Currently AceBase uses DebugLogger from acebase-core which does all the logging stuff. I suggest to add an option to acebase, acebase-client and acebase-server for providing your own DebugLogger compatible implementation. So it could look like:

const base = new AceBaseServer("default", {
  host: "localhost",
  port: 8080,
  logger: {
    verbose: (...args) => console.log(...args),
    log: (...args) => console.log(...args),
    warn: (...args) => console.warn(...args),
    error: (...args) => console.error(...args),
    write: (...args) => console.log(...args),
  }
});

Note, that your don't specify the log level here as you are handling all the levels yourself. If an override for a level is not specified, it will not be logged. @appy-one, let me know if you want me to work on PR for this. I guess we'll have to modify all the core, server, client and this package for it to work consistently.

@SilentAntenna if you need a workaround right now, you can write this before instantiating your AceBase client/server:

import { DebugLogger } from "acebase-core";

const logger = {
  verbose: (...args) => console.log(...args),
  log: (...args) => console.log(...args),
  warn: (...args) => console.warn(...args),
  error: (...args) => console.error(...args),
  write: (...args) => console.log(...args),
  setLevel: () => {},
};
Object.assign(DebugLogger.prototype, logger);

With this hack you can override the DebugLogger behavior with your implementation. Note, that you have to specify an empty function for setLevel, otherwise your custom behavior will be overwritten upon initialization.

appy-one commented 1 year ago

I think this a pretty good workaround proposed by @Azarattum