Effect CommunityEC
Effect Community3mo ago
6 replies
Stephen Bluck

Adding a prefix to log messages

How does one add a prefix to log messages without knowing what logger is being used? I tried something like this but it doesn't do quite what I am intending.

export const withPrefix = (prefix: string): Layer.Layer<never> =>
  Logger.replace(
    Logger.defaultLogger, // I thought this would grab whatever current logger is being used
    Logger.make(options => Logger.defaultLogger.log({ ...options, message: `[${prefix}] ${options.message}` }))
  );


The API I am sort of looking for is:

MySevice.layer.pipe(
  Layer.provide(MyLogger.withPrefix("MyServicePrefix"))
  Layer.provide(Logger.pretty)
)

So I would like to use whatever logger is provided whether that be pretty, json variants.

I know I could do the following but was hoping the above would be possible somehow
MyLogger.withPrefix(Logger.pretty, "MyServicePrefix")
Was this page helpful?