github.com/terraform-linters/tflint-plugin-sdk@v0.22.0/logger/logger.go (about)

     1  package logger
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/hashicorp/go-hclog"
     7  )
     8  
     9  // internalLogger is intended to be called via the public methods of the package.
    10  // So the output line will be the caller of this package.
    11  var internalLogger hclog.Logger
    12  
    13  // logger is inteded to be called directly.
    14  // It is mainly assumed to be used by go-plugin.
    15  var logger hclog.Logger
    16  
    17  // Use the init process to set the global logger.
    18  // It is expected to be initialized when the plugin starts
    19  // and you need to import the package in the proper order.
    20  func init() {
    21  	level := os.Getenv("TFLINT_LOG")
    22  	if level == "" {
    23  		// Do not emit logs by default
    24  		level = "off"
    25  	}
    26  
    27  	internalLogger = hclog.New(&hclog.LoggerOptions{
    28  		Level:                    hclog.LevelFromString(level),
    29  		Output:                   os.Stderr,
    30  		TimeFormat:               "15:04:05",
    31  		IncludeLocation:          true,
    32  		AdditionalLocationOffset: 1,
    33  	})
    34  	logger = hclog.New(&hclog.LoggerOptions{
    35  		Level:           hclog.LevelFromString(level),
    36  		Output:          os.Stderr,
    37  		TimeFormat:      "15:04:05",
    38  		IncludeLocation: true,
    39  	})
    40  }
    41  
    42  // Logger returns hcl.Logger
    43  func Logger() hclog.Logger {
    44  	return logger
    45  }
    46  
    47  // Trace emits a message at the TRACE level
    48  func Trace(msg string, args ...interface{}) {
    49  	if internalLogger == nil {
    50  		return
    51  	}
    52  	internalLogger.Trace(msg, args...)
    53  }
    54  
    55  // Debug emits a message at the DEBUG level
    56  func Debug(msg string, args ...interface{}) {
    57  	if internalLogger == nil {
    58  		return
    59  	}
    60  	internalLogger.Debug(msg, args...)
    61  }
    62  
    63  // Info emits a message at the INFO level
    64  func Info(msg string, args ...interface{}) {
    65  	if internalLogger == nil {
    66  		return
    67  	}
    68  	internalLogger.Info(msg, args...)
    69  }
    70  
    71  // Warn emits a message at the WARN level
    72  func Warn(msg string, args ...interface{}) {
    73  	if internalLogger == nil {
    74  		return
    75  	}
    76  	internalLogger.Warn(msg, args...)
    77  }
    78  
    79  // Error emits a message at the ERROR level
    80  func Error(msg string, args ...interface{}) {
    81  	if internalLogger == nil {
    82  		return
    83  	}
    84  	internalLogger.Error(msg, args...)
    85  }