@@ -24,7 +24,7 @@ module Cardano.DbSync (
24
24
extractSyncOptions ,
25
25
) where
26
26
27
- import Cardano.BM.Trace (Trace , logError , logInfo , logWarning )
27
+ import Cardano.BM.Trace (Trace )
28
28
import qualified Cardano.Crypto as Crypto
29
29
import qualified Cardano.Db as DB
30
30
import qualified Cardano.Db as Db
@@ -44,6 +44,7 @@ import Cardano.DbSync.Sync (runSyncNodeClient)
44
44
import Cardano.DbSync.Tracing.ToObjectOrphans ()
45
45
import Cardano.DbSync.Types
46
46
import Cardano.DbSync.Util.Constraint (queryIsJsonbInSchema )
47
+ import Cardano.DbSync.Util.Logging (LogContext (.. ), initLogCtx , logErrorCtx , logInfoCtx , logWarningCtx )
47
48
import Cardano.Prelude hiding (Nat , (%) )
48
49
import Cardano.Slotting.Slot (EpochNo (.. ))
49
50
import Control.Concurrent.Async
@@ -79,41 +80,49 @@ runDbSync ::
79
80
Bool ->
80
81
IO ()
81
82
runDbSync 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}
83
85
84
86
-- Read the PG connection info
85
87
pgConfig <- runOrThrowIO (Db. readPGPass $ enpPGPassSource params)
86
88
87
89
mErrors <- liftIO $ Db. validateMigrations dbMigrationDir knownMigrations
88
90
whenJust mErrors $ \ (unknown, stage4orNewStage3) ->
89
91
if stage4orNewStage3
90
- then logWarning trce $ Db. renderMigrationValidateError unknown
92
+ then logWarningCtx trce $ logCtx {lcMessage = Db. renderMigrationValidateError unknown}
91
93
else do
92
94
let msg = Db. renderMigrationValidateError unknown
93
- logError trce msg
95
+ logErrorCtx trce $ logCtx {lcMessage = msg}
94
96
throwIO unknown
95
97
96
- logInfo trce " Schema migration files validated"
98
+ logInfoCtx trce $ logCtx {lcMessage = " Schema migration files validated" }
97
99
98
100
let runMigration mode = do
99
101
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}
103
105
Db. runMigrations pgConfig True dbMigrationDir (Just $ Db. LogFileDir " /tmp" ) mode (txOutConfigToTableType txOutConfig)
104
106
(ranMigrations, unofficial) <- if enpForceIndexes params then runMigration Db. Full else runMigration Db. Initial
105
107
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}
109
110
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
+ }
113
118
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
+ }
117
126
118
127
let connectionString = Db. toConnectionString pgConfig
119
128
@@ -162,12 +171,16 @@ runSyncNode ::
162
171
SyncOptions ->
163
172
IO ()
164
173
runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
174
+ let logCtx = initLogCtx " runSyncNode" " Cardano.DbSync"
165
175
whenJust maybeLedgerDir $
166
176
\ enpLedgerStateDir -> do
167
177
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)}
171
184
172
185
let useLedger = shouldUseLedger (sioLedger $ dncInsertOptions syncNodeConfigFromFile)
173
186
@@ -193,16 +206,16 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc
193
206
194
207
-- Warn the user that jsonb datatypes are being removed from the database schema.
195
208
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." }
197
210
liftIO $ runRemoveJsonbFromSchema syncEnv
198
211
199
212
-- Warn the user that jsonb datatypes are being added to the database schema.
200
213
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." }
202
215
liftIO $ runAddJsonbToSchema syncEnv
203
216
liftIO $ runExtraMigrationsMaybe syncEnv
204
217
unless useLedger $ liftIO $ do
205
- logInfo trce " Migrating to a no ledger schema"
218
+ logInfoCtx trce $ logCtx {lcMessage = " Migrating to a no ledger schema" }
206
219
Db. noLedgerMigrations backend trce
207
220
insertValidateGenesisDist syncEnv (dncNetworkName syncNodeConfigFromFile) genCfg (useShelleyInit syncNodeConfigFromFile)
208
221
@@ -227,13 +240,17 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc
227
240
maybeLedgerDir = enpMaybeLedgerStateDir syncNodeParams
228
241
229
242
logProtocolMagicId :: Trace IO Text -> Crypto. ProtocolMagicId -> ExceptT SyncNodeError IO ()
230
- logProtocolMagicId tracer pm =
243
+ logProtocolMagicId tracer pm = do
244
+ let logCtx = initLogCtx " logProtocolMagicId" " Cardano.DbSync"
231
245
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
+ }
237
254
238
255
-- -------------------------------------------------------------------------------------------------
239
256
@@ -299,10 +316,11 @@ extractSyncOptions snp aop snc =
299
316
300
317
startupReport :: Trace IO Text -> Bool -> SyncNodeParams -> IO ()
301
318
startupReport 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}
306
324
307
325
txOutConfigToTableType :: TxOutConfig -> DB. TxOutTableType
308
326
txOutConfigToTableType config = case config of
0 commit comments