cequence-io / openai-scala-client

Scala client for OpenAI API
MIT License
178 stars 20 forks source link

Configure standard logging framework #21

Closed phelps-sg closed 1 week ago

phelps-sg commented 1 year ago

Currently, e.g. openai-core/src/main/scala/io/cequence/openaiscala/service/OpenAIRetryServiceAdapter.scala uses injected functions for logging, but this makes it difficult to configure logging in the end application.

It might be a good idea to use a standard logging framework. Currently SLF4j is included as transitive dep, so we could use scala-logging.

peterbanda commented 1 year ago

I use scala-logging at the app level... will think about it

phelps-sg commented 1 year ago

There are several advantages of using a logging framework in a library:

  1. Generating debug strings can sometimes be costly if they are not simple constant literals. Typically we might use a formatted string to dump non-string values to output: e.g.

    logger.debug(s"queue head = ${queue.head}")

    Generating the string and passing it to the debug method will incur some overhead, since we need to access the queue, which ideally we would like to avoid unless debugging is enabled. Logging frameworks such as scala-logging use lazy evaluation to ensure the string is only built if debug is enabled. This avoids polluting the code with if statements.

  2. We can easily have different log levels for DEBUG, WARN, INFO etc.

  3. We can easily configure asynchronous loggers which do not block waiting for log IO to complete (synchronous logging IO can easily kill scalability in a large complex app).

  4. When we are troubleshooting issues with the library that are difficult to isolate, the bug reporter can easily enable debug logging for openai-scala-client and include debug output in their ticket.

  5. Nearly all Java and Scala libraries and frameworks use standard logging frameworks, and so users of openai-scala-client will naturally expect logging output from the library to be configurable using one of the standard configuration files.

  6. When troubleshooting my own application code it is often useful to see the what is happening at the API level, ie all the underlying requests and responses and conversation state. Yes I can generate my own log messages every time I make a call to one of the functions in openai-scala-client, but if the library did this automatically it would be save me the effort. Similarly I often find it useful to enable debug logging in e.g. Slick, JDBC driver or other DB backends, so that I can see all the statements being executed in the database. DB libraries use standard logging libraries to facilitate this- see e.g. https://www.playframework.com/documentation/2.8.x/AccessingAnSQLDatabase#How-to-configure-SQL-log-statement

peterbanda commented 1 week ago

the lib logback-classic added