Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
1d8fdc09
Commit
1d8fdc09
authored
Jun 08, 2016
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(logging): added log format option, #4590
parent
7d6cda49
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
16 deletions
+71
-16
conf/defaults.ini
+14
-4
conf/sample.ini
+27
-3
pkg/log/log.go
+30
-9
No files found.
conf/defaults.ini
View file @
1d8fdc09
...
...
@@ -251,18 +251,23 @@ templates_pattern = emails/*.html
# Use space to separate multiple modes, e.g. "console file"
mode
=
console, file
# Either "
Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "I
nfo"
level
=
I
nfo
# Either "
trace", "debug", "info", "warn", "error", "critical", default is "i
nfo"
level
=
i
nfo
# For "console" mode only
[log.console]
level
=
# Set formatting to "false" to disable color formatting of console logs
formatting
=
false
# log line format, valid options are text, console and json
format
=
console
# For "file" mode only
[log.file]
level
=
# log line format, valid options are text, console and json
format
=
text
# This enables automated log rotate(switch of following options), default is true
log_rotate
=
true
...
...
@@ -280,6 +285,10 @@ max_days = 7
[log.syslog]
level
=
# log line format, valid options are text, console and json
format
=
text
# Syslog network type and address. This can be udp, tcp, or unix. If left blank, the default unix endpoints will be used.
network
=
address
=
...
...
@@ -290,6 +299,7 @@ facility =
# Syslog tag. By default, the process' argv[0] is used.
tag
=
#################################### AMQP Event Publisher ##########################
[event_publisher]
enabled
=
false
...
...
conf/sample.ini
View file @
1d8fdc09
...
...
@@ -230,19 +230,26 @@ check_for_updates = true
#################################### Logging ##########################
[log]
# Either "console", "file", "syslog". Default is console and file
# Use
comma to separate multiple modes, e.g. "console,
file"
# Use
space to separate multiple modes, e.g. "console
file"
;mode = console, file
# Either "
Trace", "Debug", "Info", "Warn", "Error", "Critical", default is "I
nfo"
;level =
I
nfo
# Either "
trace", "debug", "info", "warn", "error", "critical", default is "i
nfo"
;level =
i
nfo
# For "console" mode only
[log.console]
;level =
# log line format, valid options are text, console and json
;format = console
# For "file" mode only
[log.file]
;level =
# log line format, valid options are text, console and json
;format = text
# This enables automated log rotate(switch of following options), default is true
;log_rotate = true
...
...
@@ -258,6 +265,23 @@ check_for_updates = true
# Expired days of log file(delete after max days), default is 7
;max_days = 7
[log.syslog]
;level =
# log line format, valid options are text, console and json
;format = text
# Syslog network type and address. This can be udp, tcp, or unix. If left blank, the default unix endpoints will be used.
;network =
;address =
# Syslog facility. user, daemon and local0 through local7 are valid.
;facility =
# Syslog tag. By default, the process' argv[0] is used.
;tag =
#################################### AMQP Event Publisher ##########################
[event_publisher]
;enabled = false
...
...
pkg/log/log.go
View file @
1d8fdc09
...
...
@@ -13,6 +13,7 @@ import (
"gopkg.in/ini.v1"
"github.com/inconshreveable/log15"
"github.com/inconshreveable/log15/term"
)
var
Root
log15
.
Logger
...
...
@@ -82,16 +83,17 @@ func Close() {
}
var
logLevels
=
map
[
string
]
log15
.
Lvl
{
"
T
race"
:
log15
.
LvlDebug
,
"
D
ebug"
:
log15
.
LvlDebug
,
"
I
nfo"
:
log15
.
LvlInfo
,
"
W
arn"
:
log15
.
LvlWarn
,
"
E
rror"
:
log15
.
LvlError
,
"
C
ritical"
:
log15
.
LvlCrit
,
"
t
race"
:
log15
.
LvlDebug
,
"
d
ebug"
:
log15
.
LvlDebug
,
"
i
nfo"
:
log15
.
LvlInfo
,
"
w
arn"
:
log15
.
LvlWarn
,
"
e
rror"
:
log15
.
LvlError
,
"
c
ritical"
:
log15
.
LvlCrit
,
}
func
getLogLevelFromConfig
(
key
string
,
defaultName
string
,
cfg
*
ini
.
File
)
(
string
,
log15
.
Lvl
)
{
levelName
:=
cfg
.
Section
(
key
)
.
Key
(
"level"
)
.
In
(
defaultName
,
[]
string
{
"Trace"
,
"Debug"
,
"Info"
,
"Warn"
,
"Error"
,
"Critical"
})
levelName
:=
cfg
.
Section
(
key
)
.
Key
(
"level"
)
.
MustString
(
"info"
)
levelName
=
strings
.
ToLower
(
levelName
)
level
:=
getLogLevelFromString
(
levelName
)
return
levelName
,
level
}
...
...
@@ -118,10 +120,26 @@ func getFilters(filterStrArray []string) map[string]log15.Lvl {
return
filterMap
}
func
getLogFormat
(
format
string
)
log15
.
Format
{
switch
format
{
case
"console"
:
if
term
.
IsTty
(
os
.
Stdout
.
Fd
())
{
return
log15
.
TerminalFormat
()
}
return
log15
.
LogfmtFormat
()
case
"text"
:
return
log15
.
LogfmtFormat
()
case
"json"
:
return
log15
.
JsonFormat
()
default
:
return
log15
.
LogfmtFormat
()
}
}
func
ReadLoggingConfig
(
modes
[]
string
,
logsPath
string
,
cfg
*
ini
.
File
)
{
Close
()
defaultLevelName
,
_
:=
getLogLevelFromConfig
(
"log"
,
"
I
nfo"
,
cfg
)
defaultLevelName
,
_
:=
getLogLevelFromConfig
(
"log"
,
"
i
nfo"
,
cfg
)
defaultFilters
:=
getFilters
(
cfg
.
Section
(
"log"
)
.
Key
(
"filters"
)
.
Strings
(
" "
))
handlers
:=
make
([]
log15
.
Handler
,
0
)
...
...
@@ -136,18 +154,20 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
// Log level.
_
,
level
:=
getLogLevelFromConfig
(
"log."
+
mode
,
defaultLevelName
,
cfg
)
modeFilters
:=
getFilters
(
sec
.
Key
(
"filters"
)
.
Strings
(
" "
))
format
:=
getLogFormat
(
sec
.
Key
(
"format"
)
.
MustString
(
""
))
var
handler
log15
.
Handler
// Generate log configuration.
switch
mode
{
case
"console"
:
handler
=
log15
.
St
doutHandler
handler
=
log15
.
St
reamHandler
(
os
.
Stdout
,
format
)
case
"file"
:
fileName
:=
sec
.
Key
(
"file_name"
)
.
MustString
(
filepath
.
Join
(
logsPath
,
"grafana.log"
))
os
.
MkdirAll
(
filepath
.
Dir
(
fileName
),
os
.
ModePerm
)
fileHandler
:=
NewFileWriter
()
fileHandler
.
Filename
=
fileName
fileHandler
.
Format
=
format
fileHandler
.
Rotate
=
sec
.
Key
(
"log_rotate"
)
.
MustBool
(
true
)
fileHandler
.
Maxlines
=
sec
.
Key
(
"max_lines"
)
.
MustInt
(
1000000
)
fileHandler
.
Maxsize
=
1
<<
uint
(
sec
.
Key
(
"max_size_shift"
)
.
MustInt
(
28
))
...
...
@@ -159,6 +179,7 @@ func ReadLoggingConfig(modes []string, logsPath string, cfg *ini.File) {
handler
=
fileHandler
case
"syslog"
:
sysLogHandler
:=
NewSyslog
()
sysLogHandler
.
Format
=
format
sysLogHandler
.
Network
=
sec
.
Key
(
"network"
)
.
MustString
(
""
)
sysLogHandler
.
Address
=
sec
.
Key
(
"address"
)
.
MustString
(
""
)
sysLogHandler
.
Facility
=
sec
.
Key
(
"facility"
)
.
MustString
(
"local7"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment