本文整理匯總了Golang中github.com/hashicorp/logutils.LogLevel函數的典型用法代碼示例。如果您正苦於以下問題:Golang LogLevel函數的具體用法?Golang LogLevel怎麽用?Golang LogLevel使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了LogLevel函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: LogOutput
// LogOutput determines where we should send logs (if anywhere) and the log level.
func LogOutput() (logOutput io.Writer, err error) {
logOutput = ioutil.Discard
envLevel := os.Getenv(EnvLog)
if envLevel == "" {
return
}
logOutput = os.Stderr
if logPath := os.Getenv(EnvLogFile); logPath != "" {
var err error
logOutput, err = os.Create(logPath)
if err != nil {
return nil, err
}
}
// This was the default since the beginning
logLevel := logutils.LogLevel("TRACE")
if isValidLogLevel(envLevel) {
// allow following for better ux: info, Info or INFO
logLevel = logutils.LogLevel(strings.ToUpper(envLevel))
} else {
log.Printf("[WARN] Invalid log level: %q. Defaulting to level: TRACE. Valid levels are: %+v",
envLevel, validLevels)
}
logOutput = &logutils.LevelFilter{
Levels: validLevels,
MinLevel: logLevel,
Writer: logOutput,
}
return
}
示例2: setupLogging
func setupLogging() {
minLevel := logutils.LogLevel("ERROR")
if *debug {
minLevel = logutils.LogLevel("DEBUG")
}
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "ERROR"},
MinLevel: minLevel,
Writer: os.Stderr,
}
log.SetOutput(filter)
}
示例3: Setup
func Setup(config *Config) error {
var logOutput io.Writer
// Setup the default logging
logFilter := NewLogFilter()
logFilter.MinLevel = logutils.LogLevel(strings.ToUpper(config.Level))
logFilter.Writer = config.Writer
if !ValidateLevelFilter(logFilter.MinLevel, logFilter) {
levels := make([]string, 0, len(logFilter.Levels))
for _, level := range logFilter.Levels {
levels = append(levels, string(level))
}
return fmt.Errorf("invalid log level %q, valid log levels are %s",
config.Level, strings.Join(levels, ", "))
}
// Check if syslog is enabled
if config.Syslog {
log.Printf("[DEBUG] (logging) enabling syslog on %s", config.SyslogFacility)
l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, config.SyslogFacility, config.Name)
if err != nil {
return fmt.Errorf("error setting up syslog logger: %s", err)
}
syslog := &SyslogWrapper{l, logFilter}
logOutput = io.MultiWriter(logFilter, syslog)
} else {
logOutput = io.MultiWriter(logFilter)
}
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.LUTC)
log.SetOutput(logOutput)
return nil
}
示例4: main
func main() {
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"},
MinLevel: logutils.LogLevel("INFO"),
Writer: os.Stderr,
}
log.SetOutput(filter)
viper.SetConfigName("Rigfile")
viper.AddConfigPath("$HOME/.rigger/")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
if _, ok := err.(viper.UnsupportedConfigError); ok {
log.Printf("[ERROR] No Rigfile exists.")
os.Exit(1)
} else {
log.Printf("[ERROR] %s", err)
}
}
var cmdUp = &cobra.Command{
Use: "up",
Short: "Create my infrastructure",
Long: `Do lots of work`,
Run: func(cmd *cobra.Command, args []string) {
log.Printf("[INFO] Rigger lifting!")
},
}
var rootCmd = &cobra.Command{Use: "rigger"}
rootCmd.AddCommand(cmdUp)
rootCmd.Execute()
}
示例5: TestIPCLogStream
func TestIPCLogStream(t *testing.T) {
sc := &MockStreamClient{}
filter := LevelFilter()
filter.MinLevel = logutils.LogLevel("INFO")
ls := newLogStream(sc, filter, 42, log.New(os.Stderr, "", log.LstdFlags))
defer ls.Stop()
log := "[DEBUG] this is a test log"
log2 := "[INFO] This should pass"
ls.HandleLog(log)
ls.HandleLog(log2)
time.Sleep(5 * time.Millisecond)
if len(sc.headers) != 1 {
t.Fatalf("expected 1 messages!")
}
for _, h := range sc.headers {
if h.Seq != 42 {
t.Fatalf("bad seq")
}
if h.Error != "" {
t.Fatalf("bad err")
}
}
obj1 := sc.objs[0].(*logRecord)
if obj1.Log != log2 {
t.Fatalf("bad event %#v", obj1)
}
}
示例6: TestSyslogFilter
func TestSyslogFilter(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "LOCAL0", "consul")
if err != nil {
t.Fatalf("err: %s", err)
}
filt := LevelFilter()
filt.MinLevel = logutils.LogLevel("INFO")
s := &SyslogWrapper{l, filt}
n, err := s.Write([]byte("[INFO] test"))
if err != nil {
t.Fatalf("err: %s", err)
}
if n == 0 {
t.Fatalf("should have logged")
}
n, err = s.Write([]byte("[DEBUG] test"))
if err != nil {
t.Fatalf("err: %s", err)
}
if n != 0 {
t.Fatalf("should not have logged")
}
}
示例7: TestSyslogFilter
func TestSyslogFilter(t *testing.T) {
if runtime.GOOS == "windows" {
t.SkipNow()
}
// Travis does not support syslog for some reason
if travis := os.Getenv("TRAVIS"); travis != "" {
t.SkipNow()
}
l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, "LOCAL0", "consul-template")
if err != nil {
t.Fatalf("err: %s", err)
}
filt := NewLogFilter()
filt.MinLevel = logutils.LogLevel("INFO")
s := &SyslogWrapper{l, filt}
n, err := s.Write([]byte("[INFO] test"))
if err != nil {
t.Fatalf("err: %s", err)
}
if n == 0 {
t.Fatalf("should have logged")
}
n, err = s.Write([]byte("[DEBUG] test"))
if err != nil {
t.Fatalf("err: %s", err)
}
if n != 0 {
t.Fatalf("should not have logged")
}
}
示例8: handleReload
// handleReload is invoked when we should reload our configs, e.g. SIGHUP
func (c *Command) handleReload(config *Config, agent *Agent) *Config {
c.Ui.Output("Reloading configuration...")
newConf := c.readConfig()
if newConf == nil {
c.Ui.Error(fmt.Sprintf("Failed to reload configs"))
return config
}
// Change the log level
minLevel := logutils.LogLevel(strings.ToUpper(newConf.LogLevel))
if ValidateLevelFilter(minLevel, c.logFilter) {
c.logFilter.SetMinLevel(minLevel)
} else {
c.Ui.Error(fmt.Sprintf(
"Invalid log level: %s. Valid log levels are: %v",
minLevel, c.logFilter.Levels))
// Keep the current log level
newConf.LogLevel = config.LogLevel
}
// Change the event handlers
c.scriptHandler.UpdateScripts(newConf.EventScripts())
// Update the tags in serf
if err := agent.SetTags(newConf.Tags); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to update tags: %v", err))
return newConf
}
return newConf
}
示例9: LogOutput
// LogOutput determines where we should send logs (if anywhere) and the log level.
func LogOutput() (logOutput io.Writer, err error) {
logOutput = ioutil.Discard
logLevel := LogLevel()
if logLevel == "" {
return
}
logOutput = os.Stderr
if logPath := os.Getenv(EnvLogFile); logPath != "" {
var err error
logOutput, err = os.OpenFile(logPath, syscall.O_CREAT|syscall.O_RDWR|syscall.O_APPEND, 0666)
if err != nil {
return nil, err
}
}
// This was the default since the beginning
logOutput = &logutils.LevelFilter{
Levels: validLevels,
MinLevel: logutils.LogLevel(logLevel),
Writer: logOutput,
}
return
}
示例10: Run
func (c *MonitorCommand) Run(args []string) int {
var logLevel string
cmdFlags := flag.NewFlagSet("monitor", flag.ContinueOnError)
cmdFlags.Usage = func() { c.Ui.Output(c.Help()) }
cmdFlags.StringVar(&logLevel, "log-level", "INFO", "log level")
rpcAddr := RPCAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
return 1
}
client, err := RPCClient(*rpcAddr)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
}
defer client.Close()
logCh := make(chan string, 1024)
monHandle, err := client.Monitor(logutils.LogLevel(logLevel), logCh)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error starting monitor: %s", err))
return 1
}
defer client.Stop(monHandle)
eventDoneCh := make(chan struct{})
go func() {
defer close(eventDoneCh)
OUTER:
for {
select {
case log := <-logCh:
if log == "" {
break OUTER
}
c.Ui.Info(log)
}
}
c.lock.Lock()
defer c.lock.Unlock()
if !c.quitting {
c.Ui.Info("")
c.Ui.Output("Remote side ended the monitor! This usually means that the\n" +
"remote side has exited or crashed.")
}
}()
select {
case <-eventDoneCh:
return 1
case <-c.ShutdownCh:
c.lock.Lock()
c.quitting = true
c.lock.Unlock()
}
return 0
}
示例11: setLogger
func setLogger(level string) {
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR", "FATAL"},
MinLevel: logutils.LogLevel(level),
Writer: os.Stderr,
}
log.SetOutput(filter)
}
示例12: handleReload
// handleReload is invoked when we should reload our configs, e.g. SIGHUP
func (c *Command) handleReload(config *Config) *Config {
c.Ui.Output("Reloading configuration...")
newConf := c.readConfig()
if newConf == nil {
c.Ui.Error(fmt.Sprintf("Failed to reload configs"))
return config
}
// Change the log level
minLevel := logutils.LogLevel(strings.ToUpper(newConf.LogLevel))
if ValidateLevelFilter(minLevel, c.logFilter) {
c.logFilter.SetMinLevel(minLevel)
} else {
c.Ui.Error(fmt.Sprintf(
"Invalid log level: %s. Valid log levels are: %v",
minLevel, c.logFilter.Levels))
// Keep the current log level
newConf.LogLevel = config.LogLevel
}
// Bulk update the services and checks
c.agent.PauseSync()
defer c.agent.ResumeSync()
// Deregister the old services
for _, service := range config.Services {
ns := service.NodeService()
c.agent.RemoveService(ns.ID)
}
// Deregister the old checks
for _, check := range config.Checks {
health := check.HealthCheck(config.NodeName)
c.agent.RemoveCheck(health.CheckID)
}
// Register the services
for _, service := range newConf.Services {
ns := service.NodeService()
chkType := service.CheckType()
if err := c.agent.AddService(ns, chkType); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to register service '%s': %v", service.Name, err))
}
}
// Register the checks
for _, check := range newConf.Checks {
health := check.HealthCheck(config.NodeName)
chkType := &check.CheckType
if err := c.agent.AddCheck(health, chkType); err != nil {
c.Ui.Error(fmt.Sprintf("Failed to register check '%s': %v %v", check.Name, err, check))
}
}
return newConf
}
示例13: validateLogLevel
func validateLogLevel(level string) error {
err := errors.New("invalid log level")
for _, l := range LOG_LEVELS {
if l == logutils.LogLevel(strings.ToUpper(level)) {
return nil
}
}
return err
}
示例14: TestLevelFilter
func TestLevelFilter(t *testing.T) {
filt := LevelFilter()
filt.Levels = []logutils.LogLevel{"TRACE", "DEBUG", "INFO", "WARN", "ERR"}
level := logutils.LogLevel("INFO")
// LevelFilter regards INFO as valid level
if !ValidateLevelFilter(level, filt) {
t.Fatalf("expected valid LogLevel, %s was invalid", level)
}
level = logutils.LogLevel("FOO")
// LevelFilter regards FOO as invalid level
if ValidateLevelFilter(level, filt) {
t.Fatalf("expected invalid LogLevel, %s was valid", level)
}
}
示例15: initLogger
// initLogger gets the log level from the environment, falling back to DEBUG if
// nothing was given.
func (cli *CLI) initLogger(level string) {
minLevel := strings.ToUpper(strings.TrimSpace(level))
if minLevel == "" {
minLevel = "WARN"
}
levelFilter.Writer = cli.errStream
levelFilter.SetMinLevel(logutils.LogLevel(level))
log.SetOutput(levelFilter)
}