Skip to content

Commit 84aa566

Browse files
committed
Add support for adjusting colors through LogSettings
This commit extends our settings type with a `Colors -> Colors` function (that can set through code, for now), which is called in all the `getColors` functions. This will allow end-user customization of `Colors` used in all logging. Concretely, I want this so that I can fix for the fact that `dim` doesn't work on GitHub Actions. This hook will allow me to easily substitute `gray` which looks the same in that environment.
1 parent 43f7949 commit 84aa566

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Blammo/src/Blammo/Logging/Colors.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ import Prelude
2020

2121
import Blammo.Logging.Internal.Colors
2222
import Blammo.Logging.Internal.Logger
23-
import Blammo.Logging.LogSettings (shouldColorHandle)
23+
import Blammo.Logging.LogSettings (adjustColors, shouldColorHandle)
2424
import Control.Lens (to, view)
2525
import Control.Monad.IO.Class (MonadIO (..))
2626
import Control.Monad.Reader (MonadReader)
2727
import System.IO (Handle, stderr, stdout)
2828

2929
-- | Return 'Colors' consistent with whatever your logging is doing
3030
getColorsLogger :: (MonadReader env m, HasLogger env) => m Colors
31-
getColorsLogger = view $ loggerL . to (getColors . lShouldColor)
31+
getColorsLogger = do
32+
f <- view $ loggerL . to (adjustColors . lLogSettings)
33+
view $ loggerL . to (f . getColors . lShouldColor)
3234

3335
-- | Return 'Colors' consistent with logging, but for 'Handle'
3436
--
@@ -49,7 +51,7 @@ getColorsHandle
4951
:: (MonadIO m, MonadReader env m, HasLogger env) => Handle -> m Colors
5052
getColorsHandle h = do
5153
ls <- view $ loggerL . to lLogSettings
52-
getColors <$> shouldColorHandle ls h
54+
adjustColors ls . getColors <$> shouldColorHandle ls h
5355

5456
-- | Short-cut for @'getColorsHandle' 'stdout'@
5557
getColorsStdout :: (MonadIO m, MonadReader env m, HasLogger env) => m Colors

Blammo/src/Blammo/Logging/LogSettings.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Blammo.Logging.LogSettings
1919
, setLogSettingsDestination
2020
, setLogSettingsFormat
2121
, setLogSettingsColor
22+
, setLogSettingsColors
2223
, setLogSettingsBreakpoint
2324
, setLogSettingsConcurrency
2425

@@ -31,13 +32,15 @@ module Blammo.Logging.LogSettings
3132
, getLogSettingsConcurrency
3233

3334
-- * Logic
35+
, adjustColors
3436
, shouldLogLevel
3537
, shouldColorAuto
3638
, shouldColorHandle
3739
) where
3840

3941
import Prelude
4042

43+
import Blammo.Logging.Internal.Colors (Colors)
4144
import Blammo.Logging.LogSettings.LogLevels (LogLevels)
4245
import qualified Blammo.Logging.LogSettings.LogLevels as LogLevels
4346
import Control.Monad.IO.Class (MonadIO (..))
@@ -50,6 +53,7 @@ data LogSettings = LogSettings
5053
, lsDestination :: LogDestination
5154
, lsFormat :: LogFormat
5255
, lsColor :: LogColor
56+
, lsColors :: Colors -> Colors
5357
, lsBreakpoint :: Int
5458
, lsConcurrency :: Maybe Int
5559
}
@@ -117,6 +121,7 @@ defaultLogSettings =
117121
, lsDestination = LogDestinationStdout
118122
, lsFormat = LogFormatTerminal
119123
, lsColor = LogColorAuto
124+
, lsColors = id
120125
, lsBreakpoint = 120
121126
, lsConcurrency = Just 1
122127
}
@@ -168,6 +173,10 @@ setLogSettingsBreakpoint x ls = ls {lsBreakpoint = x}
168173
setLogSettingsConcurrency :: Maybe Int -> LogSettings -> LogSettings
169174
setLogSettingsConcurrency x ls = ls {lsConcurrency = x}
170175

176+
-- | Set a function to modify 'Colors' used in logging
177+
setLogSettingsColors :: (Colors -> Colors) -> LogSettings -> LogSettings
178+
setLogSettingsColors f ls = ls {lsColors = f}
179+
171180
getLogSettingsLevels :: LogSettings -> LogLevels
172181
getLogSettingsLevels = lsLevels
173182

@@ -186,6 +195,9 @@ getLogSettingsBreakpoint = lsBreakpoint
186195
getLogSettingsConcurrency :: LogSettings -> Maybe Int
187196
getLogSettingsConcurrency = lsConcurrency
188197

198+
adjustColors :: LogSettings -> Colors -> Colors
199+
adjustColors = lsColors
200+
189201
shouldLogLevel :: LogSettings -> LogSource -> LogLevel -> Bool
190202
shouldLogLevel = LogLevels.shouldLogLevel . getLogSettingsLevels
191203

Blammo/src/Blammo/Logging/Logger.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ setLoggerReformat f logger =
101101
logger
102102
{ lReformat = \level bytes -> fromMaybe bytes $ do
103103
lm <- Aeson.decodeStrict bytes
104-
pure $ f (lLogSettings logger) (getColors $ lShouldColor logger) level lm
104+
let g = adjustColors $ lLogSettings logger
105+
pure $ f (lLogSettings logger) (g $ getColors $ lShouldColor logger) level lm
105106
}
106107

107108
getLoggerShouldLog :: Logger -> LogSource -> LogLevel -> Bool

0 commit comments

Comments
 (0)