...

Source file src/scheduler/log/log.go

Documentation: scheduler/log

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  // Package log implements the logging.
    20  package log
    21  
    22  import (
    23  	"github.com/op/go-logging"
    24  	"os"
    25  )
    26  
    27  var logEnv = "production"
    28  
    29  // Log is the main object for accessing logging services
    30  var Log = logging.MustGetLogger("scheduler")
    31  
    32  var logTerminalFormat = logging.MustStringFormatter(
    33  	`%{color}%{time} %{level:.4s}/%{shortpkg}.%{shortfunc}%{color:reset} %{message}`,
    34  )
    35  var logTerminalProductionFormat = logging.MustStringFormatter(
    36  	`%{time} %{shortfunc} > %{level:.4s} %{id:03x} %{message}`,
    37  )
    38  
    39  func init() {
    40  	stdoutBackend := logging.NewLogBackend(os.Stdout, "", 0)
    41  	stderrBackend := logging.NewLogBackend(os.Stderr, "", 0)
    42  
    43  	if os.Getenv("P2PFAAS_LOG_ENV") == "development" {
    44  		logEnv = "development"
    45  	}
    46  	// in production no color and level error
    47  	if logEnv == "production" {
    48  		stderrBackendFormatted := logging.NewBackendFormatter(stderrBackend, logTerminalProductionFormat)
    49  		stderrBackendLeveled := logging.AddModuleLevel(stderrBackendFormatted)
    50  		stderrBackendLeveled.SetLevel(logging.INFO, "")
    51  		logging.SetBackend(stderrBackendLeveled)
    52  	} else {
    53  		stdoutBackendFormatted := logging.NewBackendFormatter(stdoutBackend, logTerminalFormat)
    54  		logging.SetBackend(stdoutBackendFormatted) // if production put stderrBackendLeveled
    55  	}
    56  
    57  	Log.Infof("Logging init successfully with env: %s", logEnv)
    58  
    59  	/*
    60  	 log.Debugf("debug")
    61  	 log.Info("info")
    62  	 log.Notice("notice")
    63  	 log.Warning("warning")
    64  	 log.Error("err")
    65  	 log.Critical("crit")
    66  	*/
    67  }
    68  
    69  func GetEnv() string {
    70  	return logEnv
    71  }
    72  

View as plain text