@@ -24,7 +24,7 @@ module Cardano.DbSync (
2424 extractSyncOptions ,
2525) where
2626
27- import Cardano.BM.Trace (Trace , logError , logInfo , logWarning )
27+ import Cardano.BM.Trace (Trace )
2828import qualified Cardano.Crypto as Crypto
2929import qualified Cardano.Db as DB
3030import qualified Cardano.Db as Db
@@ -44,6 +44,7 @@ import Cardano.DbSync.Sync (runSyncNodeClient)
4444import Cardano.DbSync.Tracing.ToObjectOrphans ()
4545import Cardano.DbSync.Types
4646import Cardano.DbSync.Util.Constraint (queryIsJsonbInSchema )
47+ import Cardano.DbSync.Util.Logging (LogContext (.. ), initLogCtx , logErrorCtx , logInfoCtx , logWarningCtx )
4748import Cardano.Prelude hiding (Nat , (%) )
4849import Cardano.Slotting.Slot (EpochNo (.. ))
4950import Control.Concurrent.Async
@@ -79,41 +80,49 @@ runDbSync ::
7980 Bool ->
8081 IO ()
8182runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFile abortOnPanic = do
82- logInfo trce $ textShow syncOpts
83+ let logCtx = initLogCtx " runDbSync" " Cardano.DbSync"
84+ logInfoCtx trce $ logCtx {lcMessage = " Current sync options: " <> textShow syncOpts}
8385
8486 -- Read the PG connection info
8587 pgConfig <- runOrThrowIO (Db. readPGPass $ enpPGPassSource params)
8688
8789 mErrors <- liftIO $ Db. validateMigrations dbMigrationDir knownMigrations
8890 whenJust mErrors $ \ (unknown, stage4orNewStage3) ->
8991 if stage4orNewStage3
90- then logWarning trce $ Db. renderMigrationValidateError unknown
92+ then logWarningCtx trce $ logCtx {lcMessage = Db. renderMigrationValidateError unknown}
9193 else do
9294 let msg = Db. renderMigrationValidateError unknown
93- logError trce msg
95+ logErrorCtx trce $ logCtx {lcMessage = msg}
9496 throwIO unknown
9597
96- logInfo trce " Schema migration files validated"
98+ logInfoCtx trce $ logCtx {lcMessage = " Schema migration files validated" }
9799
98100 let runMigration mode = do
99101 msg <- Db. getMaintenancePsqlConf pgConfig
100- logInfo trce $ " Running database migrations in mode " <> textShow mode
101- logInfo trce msg
102- when (mode `elem` [Db. Indexes , Db. Full ]) $ logWarning trce indexesMsg
102+ logInfoCtx trce $ logCtx {lcMessage = " Running database migrations in mode " <> textShow mode}
103+ logInfoCtx trce $ logCtx {lcMessage = msg}
104+ when (mode `elem` [Db. Indexes , Db. Full ]) $ logWarningCtx trce $ logCtx {lcMessage = indexesMsg}
103105 Db. runMigrations pgConfig True dbMigrationDir (Just $ Db. LogFileDir " /tmp" ) mode (txOutConfigToTableType txOutConfig)
104106 (ranMigrations, unofficial) <- if enpForceIndexes params then runMigration Db. Full else runMigration Db. Initial
105107 unless (null unofficial) $
106- logWarning trce $
107- " Unofficial migration scripts found: "
108- <> textShow unofficial
108+ logWarningCtx trce $
109+ logCtx {lcMessage = " Unofficial migration scripts found: " <> textShow unofficial}
109110
110- if ranMigrations
111- then logInfo trce " All migrations were executed"
112- else logInfo trce " Some migrations were not executed. They need to run when syncing has started."
111+ logInfoCtx trce $
112+ logCtx
113+ { lcMessage =
114+ if ranMigrations
115+ then " All migrations were executed"
116+ else " Some migrations were not executed. They need to run when syncing has started."
117+ }
113118
114- if enpForceIndexes params
115- then logInfo trce " All user indexes were created"
116- else logInfo trce " New user indexes were not created. They may be created later if necessary."
119+ logInfoCtx trce $
120+ logCtx
121+ { lcMessage =
122+ if enpForceIndexes params
123+ then " All user indexes were created"
124+ else " New user indexes were not created. They may be created later if necessary."
125+ }
117126
118127 let connectionString = Db. toConnectionString pgConfig
119128
@@ -162,12 +171,16 @@ runSyncNode ::
162171 SyncOptions ->
163172 IO ()
164173runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
174+ let logCtx = initLogCtx " runSyncNode" " Cardano.DbSync"
165175 whenJust maybeLedgerDir $
166176 \ enpLedgerStateDir -> do
167177 createDirectoryIfMissing True (unLedgerStateDir enpLedgerStateDir)
168- logInfo trce $ " Using byron genesis file from: " <> (show . unGenesisFile $ dncByronGenesisFile syncNodeConfigFromFile)
169- logInfo trce $ " Using shelley genesis file from: " <> (show . unGenesisFile $ dncShelleyGenesisFile syncNodeConfigFromFile)
170- logInfo trce $ " Using alonzo genesis file from: " <> (show . unGenesisFile $ dncAlonzoGenesisFile syncNodeConfigFromFile)
178+ logInfoCtx trce $
179+ logCtx {lcMessage = " Using byron genesis file from: " <> (show . unGenesisFile $ dncByronGenesisFile syncNodeConfigFromFile)}
180+ logInfoCtx trce $
181+ logCtx {lcMessage = " Using shelley genesis file from: " <> (show . unGenesisFile $ dncShelleyGenesisFile syncNodeConfigFromFile)}
182+ logInfoCtx trce $
183+ logCtx {lcMessage = " Using alonzo genesis file from: " <> (show . unGenesisFile $ dncAlonzoGenesisFile syncNodeConfigFromFile)}
171184
172185 let useLedger = shouldUseLedger (sioLedger $ dncInsertOptions syncNodeConfigFromFile)
173186
@@ -193,16 +206,16 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc
193206
194207 -- Warn the user that jsonb datatypes are being removed from the database schema.
195208 when (isJsonbInSchema && removeJsonbFromSchemaConfig) $ do
196- liftIO $ logWarning trce " Removing jsonb datatypes from the database. This can take time."
209+ liftIO $ logWarningCtx trce $ logCtx {lcMessage = " Removing jsonb datatypes from the database. This can take time." }
197210 liftIO $ runRemoveJsonbFromSchema syncEnv
198211
199212 -- Warn the user that jsonb datatypes are being added to the database schema.
200213 when (not isJsonbInSchema && not removeJsonbFromSchemaConfig) $ do
201- liftIO $ logWarning trce " Adding jsonb datatypes back to the database. This can take time."
214+ liftIO $ logWarningCtx trce $ logCtx {lcMessage = " Adding jsonb datatypes back to the database. This can take time." }
202215 liftIO $ runAddJsonbToSchema syncEnv
203216 liftIO $ runExtraMigrationsMaybe syncEnv
204217 unless useLedger $ liftIO $ do
205- logInfo trce " Migrating to a no ledger schema"
218+ logInfoCtx trce $ logCtx {lcMessage = " Migrating to a no ledger schema" }
206219 Db. noLedgerMigrations backend trce
207220 insertValidateGenesisDist syncEnv (dncNetworkName syncNodeConfigFromFile) genCfg (useShelleyInit syncNodeConfigFromFile)
208221
@@ -227,13 +240,17 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc
227240 maybeLedgerDir = enpMaybeLedgerStateDir syncNodeParams
228241
229242logProtocolMagicId :: Trace IO Text -> Crypto. ProtocolMagicId -> ExceptT SyncNodeError IO ()
230- logProtocolMagicId tracer pm =
243+ logProtocolMagicId tracer pm = do
244+ let logCtx = initLogCtx " logProtocolMagicId" " Cardano.DbSync"
231245 liftIO
232- . logInfo tracer
233- $ mconcat
234- [ " NetworkMagic: "
235- , textShow (Crypto. unProtocolMagicId pm)
236- ]
246+ . logInfoCtx tracer
247+ $ logCtx
248+ { lcMessage =
249+ mconcat
250+ [ " NetworkMagic: "
251+ , textShow (Crypto. unProtocolMagicId pm)
252+ ]
253+ }
237254
238255-- -------------------------------------------------------------------------------------------------
239256
@@ -299,10 +316,11 @@ extractSyncOptions snp aop snc =
299316
300317startupReport :: Trace IO Text -> Bool -> SyncNodeParams -> IO ()
301318startupReport trce aop params = do
302- logInfo trce $ mconcat [" Version number: " , Text. pack (showVersion version)]
303- logInfo trce $ mconcat [" Git hash: " , Db. gitRev]
304- logInfo trce $ mconcat [" Enviroment variable DbSyncAbortOnPanic: " , textShow aop]
305- logInfo trce $ textShow params
319+ let logCtx = initLogCtx " runSyncNode" " Cardano.DbSync"
320+ logInfoCtx trce $ logCtx {lcMessage = mconcat [" Version number: " , Text. pack (showVersion version)]}
321+ logInfoCtx trce $ logCtx {lcMessage = mconcat [" Git hash: " , Db. gitRev]}
322+ logInfoCtx trce $ logCtx {lcMessage = mconcat [" Enviroment variable DbSyncAbortOnPanic: " , textShow aop]}
323+ logInfoCtx trce $ logCtx {lcMessage = textShow params}
306324
307325txOutConfigToTableType :: TxOutConfig -> DB. TxOutTableType
308326txOutConfigToTableType config = case config of
0 commit comments