typelevel / typelevel/log4cats

Improvement proposal: "LoggerForType", use phantom types to disambiguate loggers

Open
#439 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
416
Forks
77
Avg merge
6h 25m
Merged PRs (30d)
3

Description

Hello :)

As someone else pointed out in this issue #397, the class that shows up in traces reflects where the logger was instantiated, usually the main class of the application. In many applications, it is however desirable to have several loggers, each with a name reflecting a particular portion of code. This makes it easier to spot where an issue comes from, and allows logging backends to be configured on a per-class basis.

However, creating multiple loggers and passing them around can be a rather frustrating experience. Having multiple loggers means you have to give up using implicits to pass them around. I came up with a solution to address this issue, and believe this could be a nice improvement for this library.

The idea is to use a phantom type to bind a logger instance to a single class, and optionally use a ClassTag to conveniently instantiate named loggers. There are code examples in the repository :)

final case class LoggerForType[F[_], T](logger: Logger[T]) extends Logger[F]

I haven't yet published the work as a library, and I am willing to open a PR to include it directly in log4cats, if the maintainers find it desirable :)

Cheers, and thank you for your work

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing issue #397 and the code examples in the linked named-log4cats repository. Compare the proposed LoggerForType phantom-type API with log4cats' existing logger abstractions, then confirm the desired integration scope with maintainers; done means an agreed API and accepted implementation direction.

Written by the indexing model from the issue text.

Assessment

Tech stack
scala
Domain
observability
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.