8000 Add setLoggerReformat by pbrisbin · Pull Request #62 · freckle/blammo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add setLoggerReformat #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 19, 2025
Merged

Add setLoggerReformat #62

merged 4 commits into from
Feb 19, 2025

Conversation

pbrisbin
Copy link
Member

This refactors the contract between reformatTerminal and the
construction that happens in newLogger.

Previously, we constructed lReformat directly, which is a function of
only LogLevel and ByteString. This meant that a "reformat" function
such as reformatTerminal had to do some extra work that all such
functions would have to do:

  • Construct the Colors value if needed
  • Return that ByteString as-is if it doesn't parse as LoggedMessage

We can pull both of these responsibilities out into newLogger itself
and simplify reformatTerminal by giving it what it needs directly.

Also, by passing LogSettings entirely (instead of just the
breakpoint), we've now made its signature into something that works well
as a generic "reformat" interface.

Then, by putting that logic behind a setLoggerReformat that
newLogger uses internally, we give users a hook for assign any such
"reformat" function from the outside too:

main :: IO ()
main = do
  withLoggerEnv $ \logger -> do
    let app = App $ setLoggerReformat myReformat logger
    runAppM app -- ...

myReformat :: LogSettings -> Colors -> LogLevel -> LoggedMessage -> ByteString
myReformat = undefined

An extension like this doesn't interact with LOG_FORMAT, which may be
surprising, but it works well for situations that don't need to be
runtime-dynamic like that, such as CLIs.

chris-martin
chris-martin previously approved these changes Feb 18, 2025
This refactors the contract between `reformatTerminal` and the
construction that happens in `newLogger`.

Previously, we constructed `lReformat` directly, which is a function of
only `LogLevel` and `ByteString`. This meant that a "reformat" function
such as `reformatTerminal` had to do some extra work that all such
functions would have to do:

- Construct the `Colors` value if needed
- Return that `ByteString` as-is if it doesn't parse as `LoggedMessage`

We can pull both of these responsibilities out into `newLogger` itself
and simplify `reformatTerminal` by giving it what it needs directly.

Also, by passing `LogSettings` entirely (instead of just the
breakpoint), we've now made its signature into something that works well
as a generic "reformat" interface.

Then, by putting that logic behind a `setLoggerReformat` that
`newLogger` uses internally, we give users a hook for assign any such
"reformat" function from the outside too:

```hs
main :: IO ()
main = do
  withLoggerEnv $ \logger -> do
    let app = App $ setLoggerReformat myReformat logger
    runAppM app -- ...

myReformat :: LogSettings -> Colors -> LogLevel -> LoggedMessage -> ByteString
myReformat = undefined
```

An extension like this doesn't interact with `LOG_FORMAT`, which may be
surprising, but it works well for situations that don't need to be
runtime-dynamic like that, such as CLIs.
I'm a bit neutral on this HLint rule, but I figured it's less work to
just follow the guidance than than to configure around it.
@pbrisbin pbrisbin force-pushed the pb/set-logger-reformat branch from d03eea1 to 8d5531f Compare February 18, 2025 22:34
@pbrisbin pbrisbin enabled auto-merge (rebase) February 18, 2025 22:38
@pbrisbin pbrisbin merged commit 4c79c5a into main Feb 19, 2025
8 checks passed
@pbrisbin pbrisbin deleted the pb/set-logger-reformat branch February 19, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0