adzejs / adze

A universal, modern, and extensible logging library for JavaScript and TypeScript.
https://adzejs.com/
Apache License 2.0
213 stars 8 forks source link

Feature suggestion: intercept log data or configurable middleware #159

Closed kingllama closed 1 month ago

kingllama commented 9 months ago

I've run into a few situations while using adze where I wish I had a way to modify or intercept the log data before it is terminated. Something like this would be exceptionally useful.

adze({
  middlware: (logData) => {
    if(foo) {
      // terminate as is
      return logData
    } else {
      // do not log this request
      return null
    }
  }
});

// Or

adze().intercept((logData) => {/* do something else */}).log("log something")

This would be exceptionally handy for writing custom logging code ontop of adze. In my case this would be helpful for building a react hook where a log call needs to be placed inside of an effect.

react hook example ```Typescript const useAdzeEffect = () => { const [logDataState, setLogDataState] = useState(null); const logger = adze().intercept((currentLogData) => { if(logDataState) { return currentLogData; } else { setLogDataState(currentLogData) } }); useEffect(() => { if(logDataState) { logDataState logger.log(...logDataState.args) } }, [logData]) return logger; } const MyComponent = () => { let logger = useAdzeEffect() logger.label("MyComponent").log("Some state variable") //... } ```

Another use could be modifying the log level or isSilent keys in place in special cases, such as a function taking too long.

Slow function warning example ```Typescript function mySometimesSlowFunction() { adze().label('slowfn').time.verbose('Begin slow function') // occasionally slow code... adze().label('slowfn').timeEnd.intercept( (logData) => { if(logData.data.timeElapsed > 10000){ logData.level = 0; } return logData } ).verbose('Function complete') } ```

Going back to react, adding the ability to not log every other render when strict mode is enabled would be very handy.

React strict mode example ```Typescript function useStrictLogger() { const [countState, setCountState] = useState(0) adze().intercept( (logData) => { if(countState % 2 === 0){ return null } return logData } ).log('Rendered component') setCountState(countState + 1); } ```

And some other helpful use cases:

This is only a suggestion of course, and I realize an 'escape hatch' like this is not best practice and much of this can be accomplished by filtering in the shed. That said, from my experience using adze, not having a way to add my own modifiers has been an inconvenience when using an otherwise lovely logging library.

AJStacy commented 8 months ago

Sorry for the delayed response and thank you for taking the time to write this detailed issue. I think the concept of adding a middleware layer is definitely something to be explored. I think you've provided good enough use cases to make this worthwhile.

I have been working on version 2 off and on for a while now and I will add this feature to the roadmap!

AJStacy commented 1 month ago

This feature has been added in Adze v2.