Skip to content

Commit

Permalink
fix #255 improve log tests, simplify setLoglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfinet committed Apr 28, 2020
1 parent 7cda999 commit 399ea5e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 45 deletions.
65 changes: 26 additions & 39 deletions do.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,45 +156,36 @@ test() {
}

test_logging() {
# use Process Substitution to capture log output
# https://stackoverflow.com/questions/20017805/bash-capture-output-of-command-run-in-background

# set -b
# set -o notify
build

declare -a levels=(error warn info debug)

echo "testing loglevel set from command line"
levelcount=0
for ll in ${levels[*]}; do
# test that we can see the current level and no level below this level
coproc vpll (./vouch-proxy -logtest -loglevel ${ll} -config ./config/testing/test_config.yml)
exec 2> /dev/null # suppress process terminated messages since ubuntu's kill doesn't support `kill -0`

# echo "log level $ll vouch-proxy pid ${vpll_PID} fd ${vpll[0]}";
llpid=${vpll_PID}

# declare -a shouldnotfind=(info)
declare -a shouldnotfind=()
for (( i=0; i<${#levels[@]}; i++ )); do
if (( i > $levelcount )); then
if (( $i > $levelcount )); then
shouldnotfind+=(${levels[$i]})
fi
done

linesread=0
while read -t 1 -u ${vpll[0]} line; do
IFS=$'\n';for line in $(./vouch-proxy -logtest -loglevel ${ll} -config ./config/testing/test_config.yml); do
let "linesread+=1"
# echo "$linesread $line"
for nono in ${shouldnotfind[*]} ; do
# first line is log info
if (( $linesread > 1 )) && echo $line | grep $nono; then
echo "line should not contain $nono"
echo "$linesread $line"
echo "bad case of the nonos: $nono"
exit 1;
fi
done
# first line is log info
if (( $linesread > 1 )); then
for nono in ${shouldnotfind[*]} ; do
if echo $line | grep $nono; then
echo "error: line should not contain '$nono' when loglevel is '$ll'"
echo "$linesread $line"
exit 1;
fi
done
fi
done
let "levelcount+=1"
done
Expand All @@ -204,37 +195,33 @@ test_logging() {
levelcount=0
for ll in ${levels[*]}; do
# test that we can see the current level and no level below this level
coproc vpll (./vouch-proxy -logtest -config ./config/testing/logging_${ll}.yml )
# echo "log level $ll vouch-proxy pid ${vpll_PID} fd ${vpll[0]}";

# declare -a shouldnotfind=(info)
declare -a shouldnotfind=()
for (( i=0; i<${#levels[@]}; i++ )); do
if (( $i > $levelcount )); then
shouldnotfind+=(${levels[$i]})
fi
done

# exec 2> /dev/null # suppress process terminated messages
linesread=0
while read -t 1 -u ${vpll[0]} line; do
IFS=$'\n';for line in $(./vouch-proxy -logtest -config ./config/testing/logging_${ll}.yml); do
let "linesread+=1"
# echo "$linesread $line"
for nono in ${shouldnotfind[*]} ; do
# the first three messages are log and info when starting from the command line
if (( $linesread > 3 )) && echo $line | grep $nono; then
echo "line should not contain $nono"
echo "$linesread $line"
echo "bad case of the nonos: $nono"
exit 1;
fi
done
# the first four messages are log and info when starting from the command line
if (( $linesread > 4 )); then
# echo "$linesread $line"
for nono in ${shouldnotfind[*]} ; do
# echo "testing $nono"
if echo $line | grep $nono; then
echo "error: line should not contain '$nono' when loglevel is '$ll'"
echo "$linesread $line"
exit 1;
fi
done
fi
done
let "levelcount+=1"

done
echo "passed"
killall vouch-proxy
exit 0
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func Configure() {
// TestConfiguration Confirm the Configuration is valid
func TestConfiguration() {
if Cfg.Testing {
Logging.setLogLevel(zap.DebugLevel)
// Logging.setLogLevel(zap.DebugLevel)
Logging.setDevelopmentLogger()
}

Expand Down
13 changes: 8 additions & 5 deletions pkg/cfg/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type logging struct {
FastLogger *zap.Logger
AtomicLogLevel zap.AtomicLevel
DefaultLogLevel zapcore.Level
LogLevel zapcore.Level
}

var (
Expand Down Expand Up @@ -55,8 +54,7 @@ func init() {

func (logging) setLogLevel(lvl zapcore.Level) {
// https://github.com/uber-go/zap/blob/master/zapcore/level.go#L59
if Logging.LogLevel != lvl {
Logging.LogLevel = lvl
if Logging.AtomicLogLevel.Level() != lvl {
log.Infof("setting LogLevel to %s", lvl)
Logging.AtomicLogLevel.SetLevel(lvl)
}
Expand Down Expand Up @@ -101,7 +99,7 @@ func (logging) configure() {
Cfg.LogLevel = fmt.Sprintf("%s", Logging.DefaultLogLevel)
}

if Cfg.LogLevel != Logging.LogLevel.String() {
if Cfg.LogLevel != Logging.AtomicLogLevel.Level().String() {
// log.Errorf("Logging.configure() Logging.LogLevel %s Cfg.LogLevel %s", Logging.LogLeveLogging.String(), Cfg.LogLevel)
Logging.setLogLevelString(Cfg.LogLevel)
}
Expand All @@ -118,7 +116,12 @@ func (logging) configureFromCmdline() {

if *CmdLine.logLevel != cmdLineLoggingDefault {
Logging.setLogLevel(*CmdLine.logLevel) // defaults to Logging.DefaultLogLevel which is zap.InfoLevel
log.Error("logging configured from cmdline")
log.Info("logging configured from cmdline")
// if we're supposed to run tests, run tests and exit
if *CmdLine.logTest {
Logging.cmdlineTestLogs()
}

configured = true
}
}
Expand Down

0 comments on commit 399ea5e

Please sign in to comment.