Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
B
boilr
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
nimrod
boilr
Commits
fcad34fa
Commit
fcad34fa
authored
8 years ago
by
Tamer Tas
Browse files
Options
Downloads
Patches
Plain Diff
pkg/util/tlog: simplify log level calculation
parent
7eebc19a
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
pkg/util/tlog/log.go
+54
-20
54 additions, 20 deletions
pkg/util/tlog/log.go
with
54 additions
and
20 deletions
pkg/util/tlog/log.go
+
54
−
20
View file @
fcad34fa
...
...
@@ -29,28 +29,50 @@ const (
QuestionMark
=
"?"
)
var
(
logLevel
Level
)
// Level is a 16-bit set holding the enabled log levels.
type
Level
uint16
const
(
LevelDebug
=
32
LevelFatal
=
1
6
Level
Error
=
8
Level
Warn
=
4
LevelInfo
=
2
LevelSuccess
=
1
LevelDebug
=
1
<<
5
LevelFatal
=
1
<<
4
Level
Warn
=
1
<<
3
Level
Error
=
1
<<
2
LevelInfo
=
1
<<
1
LevelSuccess
=
1
<<
0
)
var
LogLevel
uint16
// Set enables the levels upto and including the given log level.
func
(
lvl
*
Level
)
Set
(
newLvl
Level
)
{
*
lvl
=
(
newLvl
<<
1
)
-
1
}
// Permits queries whether the given log level is enabled or not.
func
(
lvl
Level
)
Permits
(
queryLvl
Level
)
bool
{
return
lvl
&
queryLvl
>
0
}
// SetLogLevel sets the global logging level.
func
SetLogLevel
(
LogLevelString
string
)
{
switch
strings
.
ToLower
(
LogLevelString
)
{
case
"debug"
:
LogLevel
|=
(
LevelSuccess
|
LevelError
|
LevelFatal
|
LevelWarn
|
LevelInfo
|
LevelDebug
)
case
"info"
:
LogLevel
|=
(
LevelSuccess
|
LevelError
|
LevelFatal
|
LevelWarn
|
LevelInfo
)
case
"warn"
:
LogLevel
|=
(
LevelSuccess
|
LevelError
|
LevelFatal
|
LevelWarn
)
default
:
LogLevel
|=
(
LevelSuccess
|
LevelError
|
LevelFatal
)
levels
:=
map
[
string
]
Level
{
"debug"
:
LevelDebug
,
"fatal"
:
LevelFatal
,
"warn"
:
LevelWarn
,
"error"
:
LevelError
,
"info"
:
LevelInfo
,
"success"
:
LevelSuccess
,
}
newLevel
,
ok
:=
levels
[
strings
.
ToLower
(
LogLevelString
)]
if
!
ok
{
Error
(
fmt
.
Sprintf
(
"unknown log level %s"
,
LogLevelString
))
return
}
logLevel
.
Set
(
newLevel
)
}
// TODO add log levels
...
...
@@ -62,7 +84,7 @@ func coloredPrintMsg(icon string, msg string, iC color.Attribute, mC color.Attri
// Debug logs the given message as a debug message.
func
Debug
(
msg
string
)
{
if
0
==
L
ogLevel
&
LevelDebug
{
if
!
l
ogLevel
.
Permits
(
LevelDebug
)
{
return
}
...
...
@@ -71,12 +93,16 @@ func Debug(msg string) {
// Success logs the given message as a success message.
func
Success
(
msg
string
)
{
if
!
logLevel
.
Permits
(
LevelSuccess
)
{
return
}
coloredPrintMsg
(
CheckMark
,
msg
,
color
.
FgWhite
,
color
.
FgGreen
)
}
// Info logs the given message as a info message.
func
Info
(
msg
string
)
{
if
0
==
L
ogLevel
&
LevelInfo
{
if
!
l
ogLevel
.
Permits
(
LevelInfo
)
{
return
}
...
...
@@ -85,7 +111,7 @@ func Info(msg string) {
// Warn logs the given message as a warn message.
func
Warn
(
msg
string
)
{
if
0
==
L
ogLevel
&
LevelWarn
{
if
!
l
ogLevel
.
Permits
(
LevelWarn
)
{
return
}
...
...
@@ -94,11 +120,16 @@ func Warn(msg string) {
// Error logs the given message as a error message.
func
Error
(
msg
string
)
{
if
!
logLevel
.
Permits
(
LevelError
)
{
return
}
coloredPrintMsg
(
ErrorMark
,
msg
,
color
.
FgRed
,
color
.
FgRed
)
}
// Fatal logs the given message as a fatal message.
func
Fatal
(
msg
string
)
{
// Fatal level is being deprecated
Error
(
msg
)
}
...
...
@@ -112,3 +143,6 @@ func Prompt(msg string, defval interface{}) {
}
// TODO use dependency injection wrapper for fmt.Print usage in the code base
func
init
()
{
logLevel
.
Set
(
LevelError
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment