...

Source file src/discovery/log/log.go

Documentation: discovery/log

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019. 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 logging utilities
    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  
    47  	// in production no color and level error
    48  	if logEnv == "production" {
    49  		stderrBackendFormatted := logging.NewBackendFormatter(stderrBackend, logTerminalProductionFormat)
    50  		stderrBackendLeveled := logging.AddModuleLevel(stderrBackendFormatted)
    51  		stderrBackendLeveled.SetLevel(logging.INFO, "")
    52  		logging.SetBackend(stderrBackendLeveled)
    53  	} else {
    54  		stdoutBackendFormatted := logging.NewBackendFormatter(stdoutBackend, logTerminalFormat)
    55  		logging.SetBackend(stdoutBackendFormatted) // if production put stderrBackendLeveled
    56  	}
    57  
    58  	/*
    59  	 log.Debugf("debug")
    60  	 log.Info("info")
    61  	 log.Notice("notice")
    62  	 log.Warning("warning")
    63  	 log.Error("err")
    64  	 log.Critical("crit")
    65  	*/
    66  
    67  	Log.Infof("Logging init successfully with env: %s", logEnv)
    68  }
    69  
    70  func GetEnv() string {
    71  	return logEnv
    72  }
    73  

View as plain text