1 // Copyright 2019 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package baseapp 16 17 import ( 18 "io" 19 "os" 20 21 "github.com/rs/zerolog" 22 ) 23 24 // NewLogger returns a zerolog logger based on the conventions in a LoggingConfig 25 func NewLogger(c LoggingConfig) zerolog.Logger { 26 out := io.Writer(os.Stdout) 27 if c.Pretty { 28 out = zerolog.ConsoleWriter{Out: out} 29 } 30 31 logger := zerolog.New(out).With().Timestamp().Logger() 32 if c.Level == "" { 33 return logger 34 } 35 36 level, err := zerolog.ParseLevel(c.Level) 37 if err != nil { 38 logger.Warn().Msgf("Invalid log level %q, using the default level instead", c.Level) 39 return logger 40 } 41 42 return logger.Level(level) 43 } 44