github.com/facebookincubator/go-belt@v0.0.0-20230703220935-39cd348f1a38/tool/logger/adapter/logger_from.go (about)

     1  // Copyright 2022 Meta Platforms, Inc. and affiliates.
     2  //
     3  // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
     4  //
     5  // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
     6  //
     7  // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
     8  //
     9  // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    10  //
    11  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    12  
    13  package adapter
    14  
    15  import (
    16  	"github.com/facebookincubator/go-belt/tool/logger/types"
    17  )
    18  
    19  // LoggerFromAny makes the best effort to convert given logger of
    20  // an arbitrary type to logger.Logger. Returns an untyped `nil` if
    21  // was not successful.
    22  func LoggerFromAny(anyLogger any, opts ...types.Option) types.Logger {
    23  	if anyLogger == nil {
    24  		return nil
    25  	}
    26  
    27  	switch l := anyLogger.(type) {
    28  	case types.Logger:
    29  		return l
    30  	case CompactLogger:
    31  		return LoggerFromCompactLogger(l, opts...)
    32  	case types.Emitter:
    33  		return LoggerFromEmitter(l, opts...)
    34  	case Printfer:
    35  		return LoggerFromPrintfer(l, opts...)
    36  	case func(format string, args ...any):
    37  		return LoggerFromPrintf(l, opts...)
    38  	}
    39  
    40  	return nil
    41  }
    42  
    43  // LoggerFromCompactLogger provides a generic logger.Logger based on a given types.CompactLogger.
    44  func LoggerFromCompactLogger(compactLogger CompactLogger, opts ...types.Option) types.Logger {
    45  	return GenericSugar{CompactLogger: compactLogger}
    46  }
    47  
    48  // LoggerFromEmitter provides a generic logger.Logger based on a given types.Emitter.
    49  func LoggerFromEmitter(emitter types.Emitter, opts ...types.Option) types.Logger {
    50  	cfg := types.Options(opts).Config()
    51  	return LoggerFromCompactLogger(
    52  		&GenericLogger{
    53  			Emitters:      types.Emitters{emitter},
    54  			GetCallerFunc: cfg.GetCallerFunc,
    55  		},
    56  		opts...,
    57  	)
    58  }
    59  
    60  // EmitterFromPrintfer provides a naive types.Emitter based on a given Printfer.
    61  func EmitterFromPrintfer(printfer Printfer, opts ...types.Option) types.Emitter {
    62  	return PrintferEmitter{Printfer: printfer}
    63  }
    64  
    65  // EmitterFromPrintf provides a naive types.Emitter based on a given printf-function.
    66  func EmitterFromPrintf(printf func(format string, args ...any), opts ...types.Option) types.Emitter {
    67  	return EmitterFromPrintfer(printfWrap{Func: printf})
    68  }
    69  
    70  // LoggerFromPrintfer provides a generic naive logger.Logger based on a given Printfer.
    71  func LoggerFromPrintfer(printfer Printfer, opts ...types.Option) types.Logger {
    72  	return LoggerFromEmitter(EmitterFromPrintfer(printfer, opts...), opts...)
    73  }
    74  
    75  // LoggerFromPrintf provides a generic naive logger.Logger based on a given printf-function.
    76  func LoggerFromPrintf(printf func(format string, args ...any), opts ...types.Option) types.Logger {
    77  	return LoggerFromEmitter(EmitterFromPrintf(printf, opts...), opts...)
    78  }