File: level.go

package info (click to toggle)
golang-mongodb-mongo-driver 1.17.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 25,988 kB
  • sloc: perl: 533; ansic: 491; python: 432; sh: 327; makefile: 174
file content (74 lines) | stat: -rw-r--r-- 2,402 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (C) MongoDB, Inc. 2023-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

package logger

import "strings"

// DiffToInfo is the number of levels in the Go Driver that come before the
// "Info" level. This should ensure that "Info" is the 0th level passed to the
// sink.
const DiffToInfo = 1

// Level is an enumeration representing the log severity levels supported by
// the driver. The order of the logging levels is important. The driver expects
// that a user will likely use the "logr" package to create a LogSink, which
// defaults InfoLevel as 0. Any additions to the Level enumeration before the
// InfoLevel will need to also update the "diffToInfo" constant.
type Level int

const (
	// LevelOff suppresses logging.
	LevelOff Level = iota

	// LevelInfo enables logging of informational messages. These logs are
	// high-level information about normal driver behavior.
	LevelInfo

	// LevelDebug enables logging of debug messages. These logs can be
	// voluminous and are intended for detailed information that may be
	// helpful when debugging an application.
	LevelDebug
)

const (
	levelLiteralOff       = "off"
	levelLiteralEmergency = "emergency"
	levelLiteralAlert     = "alert"
	levelLiteralCritical  = "critical"
	levelLiteralError     = "error"
	levelLiteralWarning   = "warning"
	levelLiteralNotice    = "notice"
	levelLiteralInfo      = "info"
	levelLiteralDebug     = "debug"
	levelLiteralTrace     = "trace"
)

var LevelLiteralMap = map[string]Level{
	levelLiteralOff:       LevelOff,
	levelLiteralEmergency: LevelInfo,
	levelLiteralAlert:     LevelInfo,
	levelLiteralCritical:  LevelInfo,
	levelLiteralError:     LevelInfo,
	levelLiteralWarning:   LevelInfo,
	levelLiteralNotice:    LevelInfo,
	levelLiteralInfo:      LevelInfo,
	levelLiteralDebug:     LevelDebug,
	levelLiteralTrace:     LevelDebug,
}

// ParseLevel will check if the given string is a valid environment variable
// for a logging severity level. If it is, then it will return the associated
// driver's Level. The default Level is “LevelOff”.
func ParseLevel(str string) Level {
	for literal, level := range LevelLiteralMap {
		if strings.EqualFold(literal, str) {
			return level
		}
	}

	return LevelOff
}