Naveen Sampath | 04696f7 | 2022-06-13 15:19:14 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | package thrift |
| 21 | |
| 22 | import ( |
| 23 | "log" |
| 24 | "os" |
| 25 | ) |
| 26 | |
| 27 | // Logger is a simple wrapper of a logging function. |
| 28 | // |
| 29 | // In reality the users might actually use different logging libraries, and they |
| 30 | // are not always compatible with each other. |
| 31 | // |
| 32 | // Logger is meant to be a simple common ground that it's easy to wrap whatever |
| 33 | // logging library they use into. |
| 34 | // |
| 35 | // See https://issues.apache.org/jira/browse/THRIFT-4985 for the design |
| 36 | // discussion behind it. |
| 37 | type Logger func(msg string) |
| 38 | |
| 39 | // NopLogger is a Logger implementation that does nothing. |
| 40 | func NopLogger(msg string) {} |
| 41 | |
| 42 | // StdLogger wraps stdlib log package into a Logger. |
| 43 | // |
| 44 | // If logger passed in is nil, it will fallback to use stderr and default flags. |
| 45 | func StdLogger(logger *log.Logger) Logger { |
| 46 | if logger == nil { |
| 47 | logger = log.New(os.Stderr, "", log.LstdFlags) |
| 48 | } |
| 49 | return func(msg string) { |
| 50 | logger.Print(msg) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func fallbackLogger(logger Logger) Logger { |
| 55 | if logger == nil { |
| 56 | return StdLogger(nil) |
| 57 | } |
| 58 | return logger |
| 59 | } |