Skip to content

Instantly share code, notes, and snippets.

@KEINOS
Last active December 21, 2025 15:33
Show Gist options
  • Select an option

  • Save KEINOS/cb4dbde222fb494405bb522692235456 to your computer and use it in GitHub Desktop.

Select an option

Save KEINOS/cb4dbde222fb494405bb522692235456 to your computer and use it in GitHub Desktop.
[Golang][BadgerDB] How to suppress/hide error messages

[Golang] How To Suppress/Hide Error/INFO Messages In Badger DB

TL; DR

Set Logger field of badger.Options object to nil

pathDirDB := GetMyPathDirDB()
optionsDB := badger.DefaultOptions(pathDirDB)
if !IsModeDebug {
  optionsDB.Logger = nil // <-- Here!!!
}
myDB, err := badger.Open(optionsDB)

TS; DR

BadgerDB outputs an INFO log by default if the key doesn't exist.

func getValue(myKey []byte) ([]byte, error) {
	var (
		valCopy []byte
		err     error
	)

	err = myDB.View(func(txn *badger.Txn) error {
		valCopy = []byte("")

		// Get looks for key and returns corresponding Item.
		// If key is not found, ErrKeyNotFound is returned.
		// It will output INFO by default as well.
		item, err := txn.Get(myKey)
		if err != nil {
			return err
		}

		err = item.Value(func(val []byte) error {
			valCopy = append([]byte{}, val...)

			return nil
		})

		return err
	})

	return valCopy, err
}

The output will be something like below.

badger 2021/06/24 03:56:15 INFO: All 1 tables opened in 0s
badger 2021/06/24 03:56:15 INFO: Discard stats nextEmptySlot: 0
badger 2021/06/24 03:56:15 INFO: Set nextTxnTs to 1
badger 2021/06/24 03:56:15 INFO: Deleting empty file: /tmp/SampleDB/000001.vlog
badger 2021/06/24 03:56:15 INFO: Lifetime L0 stalled for: 0s
badger 2021/06/24 03:56:15 INFO: 
Level 0 [ ]: NumTables: 01. Size: 351 B of 0 B. Score: 0.00->0.00 StaleData: 0 B Target FileSize: 64 MiB
Level 1 [ ]: NumTables: 00. Size: 0 B of 10 MiB. Score: 0.00->0.00 StaleData: 0 B Target FileSize: 2.0 MiB
Level 2 [ ]: NumTables: 00. Size: 0 B of 10 MiB. Score: 0.00->0.00 StaleData: 0 B Target FileSize: 2.0 MiB
Level 3 [ ]: NumTables: 00. Size: 0 B of 10 MiB. Score: 0.00->0.00 StaleData: 0 B Target FileSize: 2.0 MiB
Level 4 [ ]: NumTables: 00. Size: 0 B of 10 MiB. Score: 0.00->0.00 StaleData: 0 B Target FileSize: 2.0 MiB
Level 5 [ ]: NumTables: 00. Size: 0 B of 10 MiB. Score: 0.00->0.00 StaleData: 0 B Target FileSize: 2.0 MiB
Level 6 [B]: NumTables: 00. Size: 0 B of 10 MiB. Score: 0.00->0.00 StaleData: 0 B Target FileSize: 2.0 MiB
Level Done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment