Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3
Commits
9ceca989
Commit
9ceca989
authored
Jun 12, 2015
by
rht
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Decimate go-logging & replace with logrus backend
License: MIT Signed-off-by:
rht
<
rhtbot@gmail.com
>
parent
84f974ef
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
41 deletions
+38
-41
util/log.go
util/log.go
+38
-41
No files found.
util/log.go
View file @
9ceca989
...
...
@@ -3,23 +3,21 @@ package util
import
(
"os"
logging
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/
whyrusleeping/go-logging
"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/
Sirupsen/logrus
"
)
func
init
()
{
SetupLogging
()
}
var
log
=
Logger
(
"util"
)
var
ansiGray
=
"
\0
33[0;37m"
var
ansiBlue
=
"
\0
33[0;34m"
var
log
=
logrus
.
New
()
// LogFormats is a map of formats used for our logger, keyed by name.
var
LogFormats
=
map
[
string
]
string
{
"nocolor"
:
"%{time:2006-01-02 15:04:05.000000} %{level} %{module} %{shortfile}: %{message}"
,
"color"
:
ansiGray
+
"%{time:15:04:05.000} %{color}%{level:5.5s} "
+
ansiBlue
+
"%{module:10.10s}: %{color:reset}%{message} "
+
ansiGray
+
"%{shortfile}%{color:reset}"
,
// TODO: write custom TextFormatter (don't print module=name explicitly) and
// fork logrus to add shortfile
var
LogFormats
=
map
[
string
]
*
logrus
.
TextFormatter
{
"nocolor"
:
&
logrus
.
TextFormatter
{
DisableColors
:
true
,
FullTimestamp
:
true
,
TimestampFormat
:
"2006-01-02 15:04:05.000000"
,
DisableSorting
:
true
},
"color"
:
&
logrus
.
TextFormatter
{
DisableColors
:
false
,
FullTimestamp
:
true
,
TimestampFormat
:
"15:04:05:000"
,
DisableSorting
:
true
},
}
var
defaultLogFormat
=
"color"
...
...
@@ -30,65 +28,66 @@ const (
)
// loggers is the set of loggers in the system
var
loggers
=
map
[
string
]
*
log
ging
.
Logger
{}
var
loggers
=
map
[
string
]
*
log
rus
.
Entry
{}
// SetupLogging will initialize the logger backend and set the flags.
func
SetupLogging
()
{
f
mt
:=
LogFormats
[
os
.
Getenv
(
envLoggingFmt
)]
if
fmt
==
""
{
f
m
t
=
LogFormats
[
defaultLogFormat
]
f
ormat
,
ok
:=
LogFormats
[
os
.
Getenv
(
envLoggingFmt
)]
if
!
ok
{
f
orma
t
=
LogFormats
[
defaultLogFormat
]
}
backend
:=
logging
.
NewLogBackend
(
os
.
Stderr
,
""
,
0
)
logging
.
SetBackend
(
backend
)
logging
.
SetFormatter
(
logging
.
MustStringFormatter
(
fmt
))
log
.
Out
=
os
.
Stderr
log
.
Formatter
=
format
lvl
:=
log
ging
.
ERROR
lvl
:=
log
rus
.
ErrorLevel
if
logenv
:=
os
.
Getenv
(
envLogging
);
logenv
!=
""
{
var
err
error
lvl
,
err
=
log
ging
.
Log
Level
(
logenv
)
lvl
,
err
=
log
rus
.
Parse
Level
(
logenv
)
if
err
!=
nil
{
log
.
Debugf
(
"log
ging.Log
Level() Error: %q"
,
err
)
lvl
=
log
ging
.
ERROR
// reset to ERROR, could be undefined now(?)
log
.
Debugf
(
"log
rus.Parse
Level() Error: %q"
,
err
)
lvl
=
log
rus
.
ErrorLevel
// reset to ERROR, could be undefined now(?)
}
}
Debug
=
GetenvBool
(
"IPFS_DEBUG"
)
if
Debug
{
lvl
=
logging
.
DEBUG
if
Debug
:=
GetenvBool
(
"IPFS_DEBUG"
);
Debug
{
lvl
=
logrus
.
DebugLevel
}
SetAllLoggers
(
lvl
)
}
// SetDebugLogging calls SetAllLoggers with log
ging.DEBUG
// SetDebugLogging calls SetAllLoggers with log
rus.DebugLevel
func
SetDebugLogging
()
{
SetAllLoggers
(
log
ging
.
DEBUG
)
SetAllLoggers
(
log
rus
.
DebugLevel
)
}
// SetAllLoggers changes the log
ging
.Level of all loggers to lvl
func
SetAllLoggers
(
lvl
log
ging
.
Level
)
{
log
ging
.
SetLevel
(
lvl
,
""
)
for
n
:=
range
loggers
{
logg
ing
.
SetLevel
(
lvl
,
n
)
// SetAllLoggers changes the log
rus
.Level of all loggers to lvl
func
SetAllLoggers
(
lvl
log
rus
.
Level
)
{
log
.
Level
=
lvl
for
_
,
logger
:=
range
loggers
{
logg
er
.
Level
=
lvl
}
}
// Logger retrieves a particular logger
func
Logger
(
name
string
)
*
logging
.
Logger
{
log
:=
logging
.
MustGetLogger
(
name
)
log
.
ExtraCalldepth
=
1
loggers
[
name
]
=
log
return
log
func
Logger
(
name
string
)
*
logrus
.
Entry
{
if
len
(
name
)
==
0
{
log
.
Warnf
(
"Missing name parameter"
)
name
=
"undefined"
}
if
_
,
ok
:=
loggers
[
name
];
!
ok
{
loggers
[
name
]
=
log
.
WithField
(
"module"
,
name
)
}
return
loggers
[
name
]
}
// SetLogLevel changes the log level of a specific subsystem
// name=="*" changes all subsystems
func
SetLogLevel
(
name
,
level
string
)
error
{
lvl
,
err
:=
log
ging
.
Log
Level
(
level
)
lvl
,
err
:=
log
rus
.
Parse
Level
(
level
)
if
err
!=
nil
{
return
err
}
...
...
@@ -100,13 +99,11 @@ func SetLogLevel(name, level string) error {
}
// Check if we have a logger by that name
// logging.SetLevel() can't tell us...
_
,
ok
:=
loggers
[
name
]
if
!
ok
{
if
_
,
ok
:=
loggers
[
name
];
!
ok
{
return
ErrNoSuchLogger
}
logg
ing
.
SetLevel
(
lvl
,
name
)
logg
ers
[
name
]
.
Level
=
lvl
return
nil
}
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