예제 #1
0
func (v *LogLevelValue) String() string {
	return logging.Level(*v).String()
}
예제 #2
0
func init() {
	gohanLoggingInit := func(env *Environment) {
		vm := env.VM

		builtins := map[string]interface{}{
			"gohan_log": func(call otto.FunctionCall) otto.Value {
				VerifyCallArguments(&call, "gohan_log", 3)

				// TODO:
				// Taking this as an argument is a workaround
				// for Otto returning stale values of variables
				// that have been changed in javascript.
				// We can get this from LOG_MODULE javascript
				// variable if we fix the problem.
				module, err := GetString(call.Argument(0))
				if err != nil {
					ThrowOttoException(&call, "Log module: %v", err)
				}
				logger := logging.MustGetLogger(module)

				intLevel, err := GetInt64(call.Argument(1))
				if err != nil {
					ThrowOttoException(&call, "Log level: %v", err)
				}
				level := logging.Level(intLevel)

				message, err := GetString(call.Argument(2))
				if err != nil {
					ThrowOttoException(&call, "Message: %v", err)
				}

				logGeneral(logger, level, message)

				return otto.Value{}
			},
		}
		for name, object := range builtins {
			vm.Set(name, object)
		}

		// op/go-logging/level.go has levelNames[], but it's unexported
		logLevels := map[string]logging.Level{
			"CRITICAL": logging.CRITICAL,
			"ERROR":    logging.ERROR,
			"WARNING":  logging.WARNING,
			"NOTICE":   logging.NOTICE,
			"INFO":     logging.INFO,
			"DEBUG":    logging.DEBUG,
		}
		vm.Set("LOG_LEVEL", logLevels)

		vm.Set("LOG_MODULE", "gohan.extension."+env.Name)

		err := env.Load("<Gohan logging built-ins>", `
		function gohan_log_module_push(new_module){
		    var old_module = LOG_MODULE;
		    LOG_MODULE += "." + new_module;
		    return old_module;
		}

		function gohan_log_module_restore(old_module){
		    LOG_MODULE = old_module;
		}

		function gohan_log_critical(msg) {
		    gohan_log(LOG_MODULE, LOG_LEVEL.CRITICAL, msg);
		}

		function gohan_log_error(msg) {
		    gohan_log(LOG_MODULE, LOG_LEVEL.ERROR, msg);
		}

		function gohan_log_warning(msg) {
		    gohan_log(LOG_MODULE, LOG_LEVEL.WARNING, msg);
		}

		function gohan_log_notice(msg) {
		    gohan_log(LOG_MODULE, LOG_LEVEL.NOTICE, msg);
		}

		function gohan_log_info(msg) {
		    gohan_log(LOG_MODULE, LOG_LEVEL.INFO, msg);
		}

		function gohan_log_debug(msg) {
		    gohan_log(LOG_MODULE, LOG_LEVEL.DEBUG, msg);
		}
		`)
		if err != nil {
			log.Fatal(err)
		}
	}
	RegisterInit(gohanLoggingInit)
}
예제 #3
0
func ParseArgs() *FluentdForwarderParams {
	configFile := ""
	retryInterval := (time.Duration)(0)
	connectionTimeout := (time.Duration)(0)
	writeTimeout := (time.Duration)(0)
	flushInterval := (time.Duration)(0)
	parallelism := 0
	listenOn := ""
	forwardTo := ""
	journalGroupPath := ""
	maxJournalChunkSize := int64(16777216)
	logLevel := LogLevelValue(logging.INFO)
	sslCACertBundleFile := ""
	cpuProfileFile := ""
	logFile := ""
	metadata := ""

	flagSet := flag.NewFlagSet(progName, flag.ExitOnError)

	flagSet.StringVar(&configFile, "config", "", "configuration file")
	flagSet.DurationVar(&retryInterval, "retry-interval", 0, "retry interval in which connection is tried against the remote agent")
	flagSet.DurationVar(&connectionTimeout, "conn-timeout", MustParseDuration("10s"), "connection timeout")
	flagSet.DurationVar(&writeTimeout, "write-timeout", MustParseDuration("10s"), "write timeout on wire")
	flagSet.DurationVar(&flushInterval, "flush-interval", MustParseDuration("5s"), "flush interval in which the events are forwareded to the remote agent")
	flagSet.IntVar(&parallelism, "parallelism", 1, "Number of chunks to submit at once (for td output)")
	flagSet.StringVar(&listenOn, "listen-on", "127.0.0.1:24224", "interface address and port on which the forwarder listens")
	flagSet.StringVar(&forwardTo, "to", "fluent://127.0.0.1:24225", "host and port to which the events are forwarded")
	flagSet.StringVar(&journalGroupPath, "buffer-path", "*", "directory / path on which buffer files are created. * may be used within the path to indicate the prefix or suffix like var/pre*suf")
	flagSet.Int64Var(&maxJournalChunkSize, "buffer-chunk-limit", 16777216, "Maximum size of a buffer chunk")
	flagSet.Var(&logLevel, "log-level", "log level (defaults to INFO)")
	flagSet.StringVar(&sslCACertBundleFile, "ca-certs", "", "path to SSL CA certificate bundle file")
	flagSet.StringVar(&cpuProfileFile, "cpuprofile", "", "write CPU profile to file")
	flagSet.StringVar(&logFile, "log-file", "", "path of the log file. log will be written to stderr if unspecified")
	flagSet.StringVar(&metadata, "metadata", "", "set addtional data into record")
	flagSet.Parse(os.Args[1:])

	if configFile != "" {
		err := updateFlagsByConfig(configFile, flagSet)
		if err != nil {
			Error("%s", err.Error())
			os.Exit(1)
		}
	}

	ssl := false
	outputType := ""
	databaseName := "*"
	tableName := "*"
	apiKey := ""

	if strings.Contains(forwardTo, "//") {
		u, err := url.Parse(forwardTo)
		if err != nil {
			Error("%s", err.Error())
			os.Exit(1)
		}
		switch u.Scheme {
		case "fluent", "fluentd":
			outputType = "fluent"
			forwardTo = u.Host
		case "td+http", "td+https":
			outputType = "td"
			forwardTo = u.Host
			if u.User != nil {
				apiKey = u.User.Username()
			}
			if u.Scheme == "td+https" {
				ssl = true
			}
			p := strings.Split(u.Path, "/")
			if len(p) > 1 {
				databaseName = p[1]
			}
			if len(p) > 2 {
				tableName = p[2]
			}
		}
	} else {
		outputType = "fluent"
	}
	if outputType == "" {
		Error("Invalid output specifier")
		os.Exit(1)
	} else if outputType == "fluent" {
		if !strings.ContainsRune(forwardTo, ':') {
			forwardTo += ":24224"
		}
	}
	return &FluentdForwarderParams{
		RetryInterval:       retryInterval,
		ConnectionTimeout:   connectionTimeout,
		WriteTimeout:        writeTimeout,
		FlushInterval:       flushInterval,
		Parallelism:         parallelism,
		ListenOn:            listenOn,
		OutputType:          outputType,
		ForwardTo:           forwardTo,
		Ssl:                 ssl,
		DatabaseName:        databaseName,
		TableName:           tableName,
		ApiKey:              apiKey,
		JournalGroupPath:    journalGroupPath,
		MaxJournalChunkSize: maxJournalChunkSize,
		LogLevel:            logging.Level(logLevel),
		LogFile:             logFile,
		SslCACertBundleFile: sslCACertBundleFile,
		CPUProfileFile:      cpuProfileFile,
		Metadata:            metadata,
	}
}