func addLevelsToBackend(config *util.Config, prefix string, backend logging.LeveledBackend) { level, _ := logging.LogLevel(config.GetString(prefix+"level", defaultLogLevel)) backend.SetLevel(level, "") modules := config.GetList(prefix+"modules", []interface{}{}) for _, rawModule := range modules { module, _ := rawModule.(map[string]interface{}) moduleName, _ := module["name"].(string) moduleLevel, _ := module["level"].(string) level, err := logging.LogLevel(moduleLevel) if moduleName == "" || err != nil { continue } backend.SetLevel(level, moduleName) } }
func main() { kingpin.Version(fmt.Sprintf("%s %s", kingpin.CommandLine.Name, version)) kingpin.Parse() level, err := oplogging.LogLevel(*loglevel) if err != nil { prefixedUserError("Bad loglevel: %s", loglevel) os.Exit(1) } logging.SetLevel(level) diffCmd, err := shellwords.NewParser().Parse(*diffCommand) if err != nil { userError("Error parsing diff command %q: %s", *diffCommand, err.Error()) os.Exit(1) } tests, err := testcase.DiscoverTests(*testcasePath) if err != nil { userError(err.Error()) os.Exit(1) } if err = runTests(*logstashPath, tests, *configPaths, diffCmd, *keptEnvVars); err != nil { userError(err.Error()) os.Exit(1) } os.Exit(0) }
// convertes l.Level (string) to l.level (int) // or panics if l.Level is invalid func (l *LogConfig) initLevel() { level, err := logging.LogLevel(l.Level) if err != nil { log.Panicf("Invalid -log-level %s: %v", l.Level, err) } l.level = level }
////////////////////////////////////////////////////////////////////////////////////////////////////// // 2way of run // - 1st: cmd web // call from brower: http://localhost:8080/main/core/1418,1419,2502,2694,2932,2933,2695 // - 2nd: cmd core/graphite 1418,1419,2502,2694,2932,2933,2695 ////////////////////////////////////////////////////////////////////////////////////////////////////// func main() { LOGFILE, err := os.OpenFile(LOGPATH, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { LOG.Fatalf("Log file error: %s %s", LOGPATH, err) } defer func() { LOGFILE.WriteString(fmt.Sprintf("closing %s", time.UnixDate)) LOGFILE.Close() }() logback := logging.NewLogBackend(LOGFILE, "", 0) logformatted := logging.NewBackendFormatter(logback, LOGFORMAT) loglevel := "DEBUG" GLOGLEVEL, err := logging.LogLevel(loglevel) if err != nil { GLOGLEVEL = logging.DEBUG } logging.SetBackend(logformatted) logging.SetLevel(GLOGLEVEL, "") programName := os.Args[0:1] if len(os.Args) < 2 { HCIDS = append(HCIDS, 1418, 1419, 2502, 2694, 2932, 2933, 2695) mainExec() } else { typeStr := os.Args[1:2] if len(os.Args) >= 3 { hcidStr := os.Args[2:3] allArgs := os.Args[1:] fmt.Println(programName, typeStr, hcidStr, allArgs) arry := strings.Split(hcidStr[0], ",") for i := range arry { n, _ := strconv.Atoi(arry[i]) HCIDS = append(HCIDS, n) } } else { allArgs := os.Args[1:] fmt.Println(programName, typeStr, allArgs) } if typeStr[0] == "web" { webserver() } else { STYPE = typeStr[0] mainExec() } } fmt.Println(HCIDS) }
// SetupLOG sets up logger with the correct parameters for the whole cilium architecture. func SetupLOG(logger *l.Logger, logLevel string) { hostname, _ := os.Hostname() fileFormat := l.MustStringFormatter( `%{time:` + RFC3339Milli + `} ` + hostname + ` %{level:.4s} %{id:03x} %{shortfunc} > %{message}`, ) level, err := l.LogLevel(logLevel) if err != nil { logger.Fatal(err) } backend := l.NewLogBackend(os.Stderr, "", 0) oBF := l.NewBackendFormatter(backend, fileFormat) backendLeveled := l.SetBackend(oBF) backendLeveled.SetLevel(level, "") logger.SetBackend(backendLeveled) }
func main() { var loglevel string client := &clientImpl{doneChan: make(chan struct{})} backend := logging.NewLogBackend(os.Stderr, "", 0) logging.SetBackend(backend) formatter := logging.MustStringFormatter("[%{time:15:04:05}] %{shortfile:18s}: %{color}[%{level:-5s}]%{color:reset} %{message}") logging.SetFormatter(formatter) logger = logging.MustGetLogger(pkgName) flag.StringVar(&loglevel, "loglevel", "info", "The logging level. (Suggested values: info, debug)") flag.StringVar(&client.config.server, "server", "127.0.0.1:7050", "The RPC server to connect to.") flag.StringVar(&client.config.cmd.cmd, "cmd", "new-chain", "The action that this client is requesting via the config transaction.") flag.StringVar(&client.config.cmd.args.creationPolicy, "creationPolicy", "AcceptAllPolicy", "In case of a new-chain command, the chain createion policy this request should be validated against.") flag.StringVar(&client.config.cmd.args.chainID, "chainID", "NewChainID", "In case of a new-chain command, the chain ID to create.") flag.Parse() client.config.logLevel, _ = logging.LogLevel(strings.ToUpper(loglevel)) logging.SetLevel(client.config.logLevel, logger.Module) conn, err := grpc.Dial(client.config.server, grpc.WithInsecure()) if err != nil { logger.Fatalf("Client did not connect to %s: %v", client.config.server, err) } defer conn.Close() client.rpc = ab.NewAtomicBroadcastClient(conn) switch client.config.cmd.cmd { case "new-chain": envelope := newChainRequest(client.config.cmd.args.creationPolicy, client.config.cmd.args.chainID) logger.Infof("Requesting the creation of chain \"%s\"", client.config.cmd.args.chainID) client.broadcast(envelope) default: panic("Invalid cmd given") } }
func logSetupGlobal(logLevelStr string) { logLevel, err := logging.LogLevel(logLevelStr) if err != nil { log.Fatal("Invalid log level specified") } var formatStdout = logging.MustStringFormatter( "%{color}%{time:2006-01-02T15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{color:reset} %{message}", ) stdout := logging.NewLogBackend(os.Stdout, "", 0) formatter := logging.NewBackendFormatter(stdout, formatStdout) stdoutLeveled := logging.AddModuleLevel(formatter) stdoutLeveled.SetLevel(logLevel, "") syslogBackend, err := logging.NewSyslogBackendPriority("cluegetter", syslog.LOG_MAIL) if err != nil { Log.Fatal(err) } logging.SetBackend(syslogBackend, stdoutLeveled) }
func (c *Config) postProcess() { var err error if GenesisSignatureStr != "" { c.GenesisSignature, err = cipher.SigFromHex(GenesisSignatureStr) panicIfError(err, "Invalid Signature") } if GenesisAddressStr != "" { c.GenesisAddress, err = cipher.DecodeBase58Address(GenesisAddressStr) panicIfError(err, "Invalid Address") } if BlockchainPubkeyStr != "" { c.BlockchainPubkey, err = cipher.PubKeyFromHex(BlockchainPubkeyStr) panicIfError(err, "Invalid Pubkey") } if BlockchainSeckeyStr != "" { c.BlockchainSeckey, err = cipher.SecKeyFromHex(BlockchainSeckeyStr) panicIfError(err, "Invalid Seckey") BlockchainSeckeyStr = "" } if BlockchainSeckeyStr != "" { c.BlockchainSeckey = cipher.SecKey{} } c.DataDirectory = util.InitDataDir(c.DataDirectory) if c.WebInterfaceCert == "" { c.WebInterfaceCert = filepath.Join(c.DataDirectory, "cert.pem") } if c.WebInterfaceKey == "" { c.WebInterfaceKey = filepath.Join(c.DataDirectory, "key.pem") } if c.WalletDirectory == "" { c.WalletDirectory = filepath.Join(c.DataDirectory, "wallets/") } ll, err := logging.LogLevel(c.logLevel) panicIfError(err, "Invalid -log-level %s", c.logLevel) c.LogLevel = ll }
func SetLogging(cfg Config) (err error) { var file *os.File file = os.Stdout if cfg.Logfile != "" { file, err = os.OpenFile(cfg.Logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0600) if err != nil { log.Fatal(err) } } logBackend := logging.NewLogBackend(file, "", stdlog.LstdFlags|stdlog.Lmicroseconds|stdlog.Lshortfile) logging.SetBackend(logBackend) logging.SetFormatter(logging.MustStringFormatter("%{level}: %{message}")) lv, err := logging.LogLevel(cfg.Loglevel) if err != nil { panic(err.Error()) } logging.SetLevel(lv, "") return }
// SetLogLevel sets the package logging level func SetLogLevel(level string) { logLevel, _ := logging.LogLevel(strings.ToUpper(level)) // TODO Validate input logging.SetLevel(logLevel, logger.Module) }
func NewGraphite(method string, key string, args []byte) error { runtime.GOMAXPROCS(runtime.NumCPU()) //parse config log.Debug("%s\n", *Configfile) cfg, err = ini.LoadFile(*Configfile) if err != nil { log.Fatalf("parse config "+*Configfile+" file error: %s", err) } logfile, ok := cfg.Get("system", "logfile") if !ok { log.Fatal("'logfile' missing from 'system' section") } logDir := logfile[0:strings.LastIndex(logfile, "/")] err = os.MkdirAll(logDir, 0777) if err != nil { log.Fatalf("MkdirAll error: %s", err) } //open log file logFile, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { log.Fatalf("%s, %s ", logfile, err) } defer func() { logFile.WriteString(fmt.Sprintf("closing %s", time.UnixDate)) logFile.Close() }() logback := logging.NewLogBackend(logFile, "", 0) logformatted := logging.NewBackendFormatter(logback, logFormat) loglevel, ok := cfg.Get("system", "loglevel") if !ok { log.Fatal("'loglevel' missing from 'system' section") } loglevel = strings.ToUpper(loglevel) Gloglevel, err = logging.LogLevel(loglevel) if err != nil { Gloglevel = logging.DEBUG } logging.SetBackend(logformatted) //see what we have here log.Debug("BUILD: %s\n", Build) for name, section := range cfg { log.Debug("Section: %v\n", name) for k, v := range section { log.Debug("%v: %v\n", k, v) } } host, ok := cfg.Get("server", "host") if !ok { log.Fatal("'server' missing from 'host' section") } var port int port_str, ok := cfg.Get("server", "port") if !ok { log.Fatal("'server' missing from 'port' section") } else { v, err := strconv.Atoi(port_str) if err != nil { log.Warning("'server' missing from 'port' section") } else { port = v } } Graphite, err = graphite.NewGraphite(host, port) log.Debug("Loaded Graphite connection: %#v", Graphite) if method == "simplesend" { var meta map[string]interface{} json.Unmarshal(args, &meta) err = Graphite.SimpleSend(key, getArg(meta, "Value")) log.Debug("Loaded Graphite SimpleSend: %#v", err) } else if method == "sendmetric" { var meta map[string]interface{} json.Unmarshal(args, &meta) Metric.Name = key Metric.Value = getArg(meta, "Value") Metric.Timestamp = time.Now().Unix() err = Graphite.SendMetric(Metric) log.Debug("Loaded Graphite SendMetric: %#v", err) } else if method == "sendmetrics" { metas := make([]map[string]interface{}, 0) json.Unmarshal(args, &metas) log.Debug("metas: %#v", metas) for _, meta := range metas { Metric.Name = key Metric.Value = getArg(meta, "Value") Metric.Timestamp, _ = strconv.ParseInt(getArg(meta, "Date"), 10, 64) Metrics = append(Metrics, Metric) } log.Debug("Metrics: %#v", Metrics) err = Graphite.SendMetrics(Metrics) log.Debug("Loaded Graphite SendMetric: %#v", err) } Graphite.Disconnect() // logging.SetLevel(Gloglevel, "") if err != nil { return err } return nil }
func (v *LogLevelValue) Set(s string) error { _v, err := logging.LogLevel(s) *v = LogLevelValue(_v) return err }
// NewChaincodeSupport creates a new ChaincodeSupport instance func NewChaincodeSupport(chainname ChainName, getPeerEndpoint func() (*pb.PeerEndpoint, error), userrunsCC bool, ccstartuptimeout time.Duration, secHelper crypto.Peer) *ChaincodeSupport { pnid := viper.GetString("peer.networkId") pid := viper.GetString("peer.id") s := &ChaincodeSupport{name: chainname, runningChaincodes: &runningChaincodes{chaincodeMap: make(map[string]*chaincodeRTEnv)}, secHelper: secHelper, peerNetworkID: pnid, peerID: pid} //initialize global chain chains[chainname] = s peerEndpoint, err := getPeerEndpoint() if err != nil { chaincodeLogger.Errorf("Error getting PeerEndpoint, using peer.address: %s", err) s.peerAddress = viper.GetString("peer.address") } else { s.peerAddress = peerEndpoint.Address } chaincodeLogger.Infof("Chaincode support using peerAddress: %s\n", s.peerAddress) //peerAddress = viper.GetString("peer.address") if s.peerAddress == "" { s.peerAddress = peerAddressDefault } s.userRunsCC = userrunsCC s.ccStartupTimeout = ccstartuptimeout //TODO I'm not sure if this needs to be on a per chain basis... too lowel and just needs to be a global default ? s.chaincodeInstallPath = viper.GetString("chaincode.installpath") if s.chaincodeInstallPath == "" { s.chaincodeInstallPath = chaincodeInstallPathDefault } s.peerTLS = viper.GetBool("peer.tls.enabled") if s.peerTLS { s.peerTLSCertFile = viper.GetString("peer.tls.cert.file") s.peerTLSKeyFile = viper.GetString("peer.tls.key.file") s.peerTLSSvrHostOrd = viper.GetString("peer.tls.serverhostoverride") } kadef := 0 if ka := viper.GetString("chaincode.keepalive"); ka == "" { s.keepalive = time.Duration(kadef) * time.Second } else { t, terr := strconv.Atoi(ka) if terr != nil { chaincodeLogger.Errorf("Invalid keepalive value %s (%s) defaulting to %d", ka, terr, kadef) t = kadef } else if t <= 0 { chaincodeLogger.Debugf("Turn off keepalive(value %s)", ka) t = kadef } s.keepalive = time.Duration(t) * time.Second } viper.SetEnvPrefix("CORE") viper.AutomaticEnv() replacer := strings.NewReplacer(".", "_") viper.SetEnvKeyReplacer(replacer) chaincodeLogLevelString := viper.GetString("logging.chaincode") chaincodeLogLevel, err := logging.LogLevel(chaincodeLogLevelString) if err == nil { s.chaincodeLogLevel = chaincodeLogLevel.String() } else { chaincodeLogger.Infof("chaincode logging level %s is invalid. defaulting to %s\n", chaincodeLogLevelString, flogging.DefaultLoggingLevel().String()) s.chaincodeLogLevel = flogging.DefaultLoggingLevel().String() } return s }
// TODO: This should be pushed to a wrapper or module // It populated dynamic configuration, automatically converting time.Duration etc. // Any config entries not found in the structure are moved to an "Unused" field if it exists // or an error is reported if "Unused" is not available // We can then take the unused configuration dynamically at runtime based on another value func (c *Config) PopulateConfig(config interface{}, config_path string, raw_config map[string]interface{}) (err error) { vconfig := reflect.ValueOf(config) if initDefaults := vconfig.MethodByName("InitDefaults"); initDefaults.IsValid() { initDefaults.Call([]reflect.Value{}) } vconfig = vconfig.Elem() FieldLoop: for i := 0; i < vconfig.NumField(); i++ { field := vconfig.Field(i) if !field.CanSet() { continue } fieldtype := vconfig.Type().Field(i) tag := fieldtype.Tag.Get("config") mods := strings.Split(tag, ",") tag = mods[0] mods = mods[1:] for _, mod := range mods { if mod == "embed" && field.Kind() == reflect.Struct { if err = c.PopulateConfig(field.Addr().Interface(), config_path, raw_config); err != nil { return } continue FieldLoop } } if tag == "" { continue } if field.Kind() == reflect.Struct { if _, ok := raw_config[tag]; ok { if reflect.TypeOf(raw_config[tag]).Kind() != reflect.Map { err = fmt.Errorf("Option %s%s must be a hash", config_path, tag) return } } else { raw_config[tag] = map[string]interface{}{} } if err = c.PopulateConfig(field.Addr().Interface(), fmt.Sprintf("%s%s/", config_path, tag), raw_config[tag].(map[string]interface{})); err != nil { return } delete(raw_config, tag) } else if _, ok := raw_config[tag]; ok { value := reflect.ValueOf(raw_config[tag]) if value.Type().AssignableTo(field.Type()) { field.Set(value) } else if value.Kind() == reflect.Slice && field.Kind() == reflect.Slice { if err = c.populateConfigSlice(field, fmt.Sprintf("%s%s/", config_path, tag), raw_config[tag].([]interface{})); err != nil { return } } else if value.Kind() == reflect.Map && field.Kind() == reflect.Map { if field.IsNil() { field.Set(reflect.MakeMap(field.Type())) } for _, j := range value.MapKeys() { item := value.MapIndex(j) if item.Elem().Type().AssignableTo(field.Type().Elem()) { field.SetMapIndex(j, item.Elem()) } else { err = fmt.Errorf("Option %s%s[%s] must be %s or similar", config_path, tag, j, field.Type().Elem()) return } } } else if field.Type().String() == "time.Duration" { var duration float64 vduration := reflect.ValueOf(duration) fail := true if value.Type().AssignableTo(vduration.Type()) { duration = value.Float() if duration >= math.MinInt64 && duration <= math.MaxInt64 { field.Set(reflect.ValueOf(time.Duration(int64(duration)) * time.Second)) fail = false } } else if value.Kind() == reflect.String { var tduration time.Duration if tduration, err = time.ParseDuration(value.String()); err == nil { field.Set(reflect.ValueOf(tduration)) fail = false } } if fail { err = fmt.Errorf("Option %s%s must be a valid numeric or string duration", config_path, tag) return } } else if field.Type().String() == "logging.Level" { fail := true if value.Kind() == reflect.String { var llevel logging.Level if llevel, err = logging.LogLevel(value.String()); err == nil { fail = false field.Set(reflect.ValueOf(llevel)) } } if fail { err = fmt.Errorf("Option %s%s is not a valid log level (critical, error, warning, notice, info, debug)", config_path, tag) return } } else if field.Kind() == reflect.Int64 { fail := true if value.Kind() == reflect.Float64 { number := value.Float() if math.Floor(number) == number { fail = false field.Set(reflect.ValueOf(int64(number))) } } if fail { err = fmt.Errorf("Option %s%s is not a valid integer", config_path, tag, field.Type()) return } } else if field.Kind() == reflect.Int { fail := true if value.Kind() == reflect.Float64 { number := value.Float() if math.Floor(number) == number { fail = false field.Set(reflect.ValueOf(int(number))) } } if fail { err = fmt.Errorf("Option %s%s is not a valid integer", config_path, tag, field.Type()) return } } else { err = fmt.Errorf("Option %s%s must be %s or similar (%s provided)", config_path, tag, field.Type(), value.Type()) return } delete(raw_config, tag) } } if unused := vconfig.FieldByName("Unused"); unused.IsValid() { if unused.IsNil() { unused.Set(reflect.MakeMap(unused.Type())) } for k, v := range raw_config { unused.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(v)) } return } return c.ReportUnusedConfig(config_path, raw_config) }
////////////////////////////////////////////////////////////////////////////////////////////////////// // 2way of run // - 1st: graphite_m web // call from brower: http://localhost:8080/main/core/1418,1419,2502,2694,2932,2933,2695 // - 2nd: graphite_m core/graphite 1418,1419,2502,2694,2932,2933,2695 ////////////////////////////////////////////////////////////////////////////////////////////////////// func main() { var logfile = LOGPATH if _, err := os.Stat(LOGPATH); err != nil { LOGPATH, _ := os.Getwd() logfile = LOGPATH + "/graphite_m.log" } LOGFILE, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { LOG.Fatalf("Log file error: %s %s", logfile, err) } defer func() { LOGFILE.WriteString(fmt.Sprintf("closing %s", time.UnixDate)) LOGFILE.Close() }() logback := logging.NewLogBackend(LOGFILE, "", 0) logformatted := logging.NewBackendFormatter(logback, LOGFORMAT) loglevel := "DEBUG" GLOGLEVEL, err := logging.LogLevel(loglevel) if err != nil { GLOGLEVEL = logging.DEBUG } logging.SetBackend(logformatted) logging.SetLevel(GLOGLEVEL, "") // cfg, err := ini.LoadFile(*configfile) // if err != nil { // LOG.Fatalf("parse config "+*configfile+" file error: %s", err) // } // // logfile, ok := cfg.Get("core_api_url", "logfile") // if !ok { // LOG.Fatalf("'logfile' missing from 'system' section") // } DOMAIN = readValConf(CONFIGFILE, "core_api_url") if DOMAIN == "" { DOMAIN = "http://core.local.xdn.com" } programName := os.Args[0:1] if len(os.Args) < 2 { HCIDS = append(HCIDS, 1418, 1419, 2502, 2694, 2932, 2933, 2695) mainExec() } else { typeStr := os.Args[1:2] if len(os.Args) >= 3 { hcidStr := os.Args[2:3] allArgs := os.Args[1:] fmt.Println(programName, typeStr, hcidStr, allArgs) arry := strings.Split(hcidStr[0], ",") for i := range arry { n, _ := strconv.Atoi(arry[i]) HCIDS = append(HCIDS, n) } } else { allArgs := os.Args[1:] fmt.Println(programName, typeStr, allArgs) } if typeStr[0] == "web" { webserver() } else { STYPE = typeStr[0] mainExec() } } fmt.Println(HCIDS) }