blob: 50d44ec8eae445c70ec5970edc989321a1c35e04 [file] [log] [blame]
khenaidood948f772021-08-11 17:49:24 -04001/*
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
20package thrift
21
22import (
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.
37type Logger func(msg string)
38
39// NopLogger is a Logger implementation that does nothing.
40func 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.
45func 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
54func fallbackLogger(logger Logger) Logger {
55 if logger == nil {
56 return StdLogger(nil)
57 }
58 return logger
59}