github.com/actions-on-google/gactions@v3.2.0+incompatible/log/log.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package log provides common log functions to the cli infrastructure.
    16  package log
    17  
    18  import (
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  	"runtime"
    23  
    24  	"github.com/fatih/color"
    25  )
    26  
    27  // Level defines the supported log levels.
    28  type Level uint8
    29  
    30  // Levels are the currently defined log levels for the CLI.
    31  const (
    32  	DebugLevel Level = iota // DebugLevel will be set if the debug version of the binary gets built.
    33  	InfoLevel               // InfoLevel will be set if developer passes a verbose flag.
    34  	WarnLevel
    35  	ErrorLevel
    36  	FatalLevel
    37  	PanicLevel
    38  )
    39  
    40  var (
    41  	// DebugLogger will reveal debug info which can be internal; will not be part of public binary
    42  	DebugLogger = log.New(os.Stdout, colorMaybe("[DEBUG] ", color.HiBlueString), log.Ldate|log.Ltime|log.Llongfile)
    43  	// InfoLogger sends useful but verbose information. Only sends if severity is >= InfoLevel.
    44  	InfoLogger = log.New(os.Stdout, "[INFO] ", log.Ldate|log.Ltime)
    45  	// OutLogger sends an important output from execution of the command, intended for a user to read.
    46  	OutLogger = log.New(os.Stdout, "", 0)
    47  	// WarnLogger sends warnings to stderr.
    48  	WarnLogger = log.New(os.Stderr, colorMaybe("[WARNING] ", color.YellowString), 0)
    49  	// ErrorLogger sends errors to stderr.
    50  	ErrorLogger = log.New(os.Stderr, colorMaybe("[ERROR] ", color.RedString), 0)
    51  	// Severity can be set to restrict level of log messages.
    52  	Severity = WarnLevel
    53  )
    54  
    55  func colorMaybe(s string, f func(format string, a ...interface{}) string) string {
    56  	if runtime.GOOS == "windows" {
    57  		return s
    58  	}
    59  	return f(s)
    60  }
    61  
    62  // DoneMsgln surrounds msg with helpful visual cues for the user to indicate completion of a task.
    63  func DoneMsgln(msg string) {
    64  	// Windows doesn't print special characters and colors nicely.
    65  	if runtime.GOOS == "windows" {
    66  		Outf("Done. %s\n", msg)
    67  		return
    68  	}
    69  	Outf("%v Done. %s\n", color.GreenString("✔"), msg)
    70  }
    71  
    72  // Debugf calls Output to print to the DebugLogger.
    73  // Arguments are handled in the manner of fmt.Printf.
    74  func Debugf(format string, v ...interface{}) {
    75  	if Severity > DebugLevel {
    76  		return
    77  	}
    78  	DebugLogger.Output(2, fmt.Sprintf(format, v...))
    79  }
    80  
    81  // Debugln calls Output to print to the DebugLogger.
    82  // Arguments are handled in the manner of fmt.Println.
    83  func Debugln(v ...interface{}) {
    84  	if Severity > DebugLevel {
    85  		return
    86  	}
    87  	DebugLogger.Output(2, fmt.Sprintln(v...))
    88  }
    89  
    90  // Out calls Output to print to the OutLogger.
    91  // Arguments are handled in the manner of fmt.Print.
    92  func Out(v ...interface{}) {
    93  	OutLogger.Output(2, fmt.Sprint(v...))
    94  }
    95  
    96  // Outf calls Output to print to the OutLogger.
    97  // Arguments are handled in the manner of fmt.Printf.
    98  func Outf(format string, v ...interface{}) {
    99  	OutLogger.Output(2, fmt.Sprintf(format, v...))
   100  }
   101  
   102  // Outln calls Output to print to the OutLogger.
   103  // Arguments are handled in the manner of fmt.Println.
   104  func Outln(v ...interface{}) {
   105  	OutLogger.Output(2, fmt.Sprintln(v...))
   106  }
   107  
   108  // Infoln calls Output to print to the InfoLogger.
   109  // Arguments are handled in the manner of fmt.Println.
   110  func Infoln(v ...interface{}) {
   111  	if Severity > InfoLevel {
   112  		return
   113  	}
   114  	InfoLogger.Output(2, fmt.Sprintln(v...))
   115  }
   116  
   117  // Infof calls Output to print to the InfoLogger.
   118  // Arguments are handled in the manner of fmt.Printf.
   119  func Infof(format string, v ...interface{}) {
   120  	if Severity > InfoLevel {
   121  		return
   122  	}
   123  	InfoLogger.Output(2, fmt.Sprintf(format, v...))
   124  }
   125  
   126  // Error calls Output to print to the ErrorLogger.
   127  // Arguments are handled in the manner of fmt.Print.
   128  func Error(v ...interface{}) {
   129  	if Severity > ErrorLevel {
   130  		return
   131  	}
   132  	ErrorLogger.Output(2, fmt.Sprint(v...))
   133  }
   134  
   135  // Errorf calls Output to print to the ErrorLogger.
   136  // Arguments are handled in the manner of fmt.Printf.
   137  func Errorf(format string, v ...interface{}) {
   138  	if Severity > ErrorLevel {
   139  		return
   140  	}
   141  	ErrorLogger.Output(2, fmt.Sprintf(format, v...))
   142  }
   143  
   144  // Warnf calls Output to print to the WarnLogger.
   145  // Arguments are handled in the manner of fmt.Printf.
   146  func Warnf(format string, v ...interface{}) {
   147  	if Severity > WarnLevel {
   148  		return
   149  	}
   150  	WarnLogger.Output(2, fmt.Sprintf(format, v...))
   151  }
   152  
   153  // Warnln calls Output to print to the WarnLogger.
   154  // Arguments are handled in the manner of fmt.Println.
   155  func Warnln(v ...interface{}) {
   156  	if Severity > WarnLevel {
   157  		return
   158  	}
   159  	WarnLogger.Output(2, fmt.Sprintln(v...))
   160  }