acebase icon indicating copy to clipboard operation
acebase copied to clipboard

Logging API support?

Open SilentAntenna opened this issue 3 years ago • 3 comments

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.

SilentAntenna avatar Aug 07 '22 13:08 SilentAntenna

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

appy-one avatar Aug 15 '22 20:08 appy-one

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.

Azarattum avatar Nov 27 '22 07:11 Azarattum

I think this a pretty good workaround proposed by @Azarattum

appy-one avatar Nov 28 '22 11:11 appy-one