blob: c42aac998b76470686219bd9fd5c34ba3c1ec9c9 [file] [log] [blame]
Yuxuan 'fishy' Wange4870a32019-10-24 13:23:30 -07001/*
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 "testing"
26)
27
28// Logger is a simple wrapper of a logging function.
29//
30// In reality the users might actually use different logging libraries, and they
31// are not always compatible with each other.
32//
33// Logger is meant to be a simple common ground that it's easy to wrap whatever
34// logging library they use into.
35//
36// See https://issues.apache.org/jira/browse/THRIFT-4985 for the design
37// discussion behind it.
38type Logger func(msg string)
39
40// NopLogger is a Logger implementation that does nothing.
41func NopLogger(msg string) {}
42
43// StdLogger wraps stdlib log package into a Logger.
44//
45// If logger passed in is nil, it will fallback to use stderr and default flags.
46func StdLogger(logger *log.Logger) Logger {
47 if logger == nil {
48 logger = log.New(os.Stderr, "", log.LstdFlags)
49 }
50 return func(msg string) {
51 logger.Print(msg)
52 }
53}
54
55// TestLogger is a Logger implementation can be used in test codes.
56//
57// It fails the test when being called.
58func TestLogger(tb testing.TB) Logger {
59 return func(msg string) {
60 tb.Errorf("logger called with msg: %q", msg)
61 }
62}
63
64func fallbackLogger(logger Logger) Logger {
65 if logger == nil {
66 return StdLogger(nil)
67 }
68 return logger
69}