當前位置: 首頁>>代碼示例>>Golang>>正文


Golang fluent.New函數代碼示例

本文整理匯總了Golang中github.com/fluent/fluent-logger-golang/fluent.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: write

func write(log map[string]string) error {
	if logFileName != "" {
		f, err := os.OpenFile(logFileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, logFilePermit)
		if err != nil {
			logger.Println("Open log failed.")
			return nil
		}
		defer f.Close()

		ltsvLog := convertLTSV(log)
		if _, err = f.WriteString(ltsvLog); err != nil {
			logger.Println("Write log failed.")
		}
	}

	if fluentTag != "" {
		logger, err := fluent.New(fluent.Config{FluentPort: fluentPort, FluentHost: fluentHost})
		if err != nil {
			return err
		}
		defer logger.Close()

		error := logger.Post(fluentTag, log)
		if error != nil {
			panic(error)
		}
	}
	return nil
}
開發者ID:TakatoshiMaeda,項目名稱:lightup,代碼行數:29,代碼來源:main.go

示例2: connectToFluentd

// connectToFluentd continuously tries to connect to Fluentd.
func connectToFluentd() *fluent.Fluent {
	var err error
	var logger *fluent.Fluent

	// Continuously try to connect to Fluentd.
	backoff := time.Duration(*fluentdRetryWait) * time.Millisecond
	for {
		log.Printf("Connecting to Fluentd (%s:%d)...", *fluentdHost, *fluentdPort)
		logger, err = fluent.New(fluent.Config{
			FluentHost: *fluentdHost,
			FluentPort: *fluentdPort,
			// Once we have a connection, the library will reconnect automatically
			// if the connection is lost. However, it panics if it fails to connect
			// more than MaxRetry times. To avoid panics crashing the server, retry
			// many times before panicking.
			MaxRetry:  240,
			RetryWait: *fluentdRetryWait,
		})
		if err != nil {
			log.Printf("Could not connect to Fluentd: %v", err)
			time.Sleep(backoff)
			backoff *= 2
		} else {
			log.Printf("Connected to Fluentd (%s:%d)...", *fluentdHost, *fluentdPort)
			return logger
		}
	}
}
開發者ID:IanLewis,項目名稱:weathersensors,代碼行數:29,代碼來源:main.go

示例3: Init

// Reads configuration and connects to fluentd
func Init() {
	var err error
	var timeout time.Duration = 0
	timeStr, exists := revel.Config.String("revfluent.timeout")
	if exists {
		timeout, err = time.ParseDuration(timeStr)
		if err != nil {
			revel.ERROR.Panic(err)
		}
	}

	var retryWait int = 0
	retryWaitStr, exists := revel.Config.String("revfluent.retryWait")
	if exists {
		tmp, err := time.ParseDuration(retryWaitStr)
		if err != nil {
			revel.ERROR.Panic(err)
		}

		retryWait = int(tmp.Nanoseconds() / 1e6)
	}

	port, _ := revel.Config.Int("revfluent.port")
	host, _ := revel.Config.String("revfluent.host")
	network, _ := revel.Config.String("revfluent.network")
	socketPath, _ := revel.Config.String("revfluent.socketPath")
	bufferLimit, _ := revel.Config.Int("revfluent.bufferLimit")
	maxRetry, _ := revel.Config.Int("revfluent.maxRetry")

	appName, _ := revel.Config.String("app.name")
	tagPrefix := revel.Config.StringDefault("revfluent.tagPrefix", appName)

	config := fluent.Config{
		FluentPort:       port,
		FluentHost:       host,
		FluentNetwork:    network,
		FluentSocketPath: socketPath,
		Timeout:          timeout,
		BufferLimit:      bufferLimit,
		RetryWait:        retryWait,
		MaxRetry:         maxRetry,
		TagPrefix:        tagPrefix,
	}

	var hostUsed string
	if hostUsed = host; host == "" {
		hostUsed = "127.0.0.1"
	}

	var portUsed int
	if portUsed = port; port == 0 {
		portUsed = 24224
	}
	revel.INFO.Printf("Connecting Fluentd: %s:%d", hostUsed, portUsed)

	Logger, err = fluent.New(config)
	if err != nil {
		revel.ERROR.Panic("Failed to connect Fluentd: %s", err)
	}
}
開發者ID:janekolszak,項目名稱:revfluent,代碼行數:61,代碼來源:revfluent.go

示例4: Fire

func (hook *fluentHook) Fire(entry *logrus.Entry) error {
	logger, err := fluent.New(fluent.Config{
		FluentHost: hook.host,
		FluentPort: hook.port,
	})
	if err != nil {
		return err
	}
	defer logger.Close()

	// Create a map for passing to FluentD
	data := make(logrus.Fields)
	for k, v := range entry.Data {
		data[k] = v
	}

	setLevelString(entry, data)
	tag := getTagAndDel(entry, data)
	if tag != entry.Message {
		setMessage(entry, data)
	}

	fluentData := ConvertToValue(data, TagName)
	err = logger.PostWithTime(tag, entry.Time, fluentData)
	return err
}
開發者ID:naganumat,項目名稱:logrus_fluent,代碼行數:26,代碼來源:fluent.go

示例5: New

// New creates a fluentd logger using the configuration passed in on
// the context. Supported context configuration variables are
// fluentd-address & fluentd-tag.
func New(ctx logger.Context) (logger.Logger, error) {
	host, port, err := parseAddress(ctx.Config["fluentd-address"])
	if err != nil {
		return nil, err
	}

	tag, err := loggerutils.ParseLogTag(ctx, "docker.{{.ID}}")
	if err != nil {
		return nil, err
	}
	extra := ctx.ExtraAttributes(nil)
	logrus.Debugf("logging driver fluentd configured for container:%s, host:%s, port:%d, tag:%s, extra:%v.", ctx.ContainerID, host, port, tag, extra)
	// logger tries to reconnect 2**32 - 1 times
	// failed (and panic) after 204 years [ 1.5 ** (2**32 - 1) - 1 seconds]
	log, err := fluent.New(fluent.Config{FluentPort: port, FluentHost: host, RetryWait: 1000, MaxRetry: math.MaxInt32})
	if err != nil {
		return nil, err
	}
	return &fluentd{
		tag:           tag,
		containerID:   ctx.ContainerID,
		containerName: ctx.ContainerName,
		writer:        log,
		extra:         extra,
	}, nil
}
開發者ID:DaveDaCoda,項目名稱:docker,代碼行數:29,代碼來源:fluentd.go

示例6: main

func main() {
	var tag = flag.String("tag", "syslog", "fleuntd tag for logging")
	var fluent_socket = flag.String("socket", "/tmp/td-agent.sock", "fleuntd socket for logging")
	var task_name = flag.String("task", "test", "task name")
	var message_subject = flag.String("subject", "subject", "message subject")
	var message_enable = flag.Bool("debug", false, "send email")

	flag.Parse()
	// fmt.Println(*tag)

	logger, err := fluent.New(fluent.Config{FluentSocketPath: *fluent_socket, FluentNetwork: "unix"})
	if err != nil {
		log.Fatal(err)
	}

	var message []string

	bio := bufio.NewReader(os.Stdin)
	for {
		line, _, err := bio.ReadLine()
		if err != nil {
			break
		}
		message = append(message, string(line))
	}
	// fmt.Println(time.Now(), "\n", strings.Join(message, "\n") )

	defer logger.Close()

	var data = map[string]string{
		"message":   strings.Join(message, "\n"),
		"timestamp": time.Now().String()}

	if err = logger.Post(*tag, data); err != nil {
		log.Fatal(err)
	}

	d := gomail.NewPlainDialer("127.0.0.1", 25, "", "")
	s, err := d.Dial()
	if err != nil {
		log.Fatal(err)
	}

	m := gomail.NewMessage()

	m.SetHeader("From", "[email protected]")
	m.SetHeader("To", "[email protected]")
	m.SetHeader("Subject", "go-fluentd-stdin")
	m.SetBody("text/html", strings.Join(message, "\n"))

	if err := gomail.Send(s, m); err != nil {
		log.Printf("Could not send email to %q: %v", "[email protected]", err)
	}
	m.Reset()

}
開發者ID:a15y87,項目名稱:go-stdin-logger,代碼行數:56,代碼來源:go-fluentd-stdin.go

示例7: main

func main() {
	var dbid = flag.String("database", "", "database identifier")
	var path = flag.String("path", "", "log file path")
	var fluentHost = flag.String("fluent-host", "", "fluentd hostname")
	var fluentPort = flag.Int("fluent-port", 24224, "fluentd forward port (default 24224)")
	var fluentTag = flag.String("fluent-tag", "mysql.slowquery", "fluentd tag")
	var raw = flag.Bool("raw", false, "output raw data (stdout only)")
	flag.Parse()

	if *dbid == "" || *path == "" {
		fmt.Println("Usage: rds-throwlog --database=[database identifier] --path=[log file path]")
		return
	}

	stream, err := Fetch(dbid, path)
	if err != nil {
		log.Println(err)
		return
	}
	defer stream.Close()
	log.Println("download completed")

	// output raw
	if *raw {
		io.Copy(os.Stdout, stream)
		return
	}

	// prepare fluent-logger
	var logger *fluent.Fluent
	if *fluentHost != "" {
		logger, err = fluent.New(fluent.Config{
			FluentPort: *fluentPort,
			FluentHost: *fluentHost,
		})
		if err != nil {
			log.Println("fluent.New returned error:", err)
			return
		}
	}

	records := mysqlslow.Parse(stream)
	for _, r := range records {
		if logger != nil {
			t, msg := r.ToFluentLog()
			log.Println(t)
			logger.PostWithTime(*fluentTag, t, msg)
		} else {
			data, err := json.Marshal(r)
			if err != nil {
				fmt.Println(err)
			}
			fmt.Println(string(data))
		}
	}
}
開發者ID:acidlemon,項目名稱:rds-throwlog,代碼行數:56,代碼來源:main.go

示例8: Start

func (p *Output) Start() error {
	gigo.Debugf(p.logger, "out_fluent: start")
	if p.output != nil {
		return fmt.Errorf("already started")
	}
	output, err := fluent.New(p.config)
	if err != nil {
		return err
	}
	p.output = output
	return nil
}
開發者ID:najeira,項目名稱:gigo,代碼行數:12,代碼來源:out_fluent.go

示例9: NewHook

func NewHook(host string, port int) (*fluentHook, error) {
	logger, err := fluent.New(fluent.Config{
		FluentHost: host,
		FluentPort: port,
	})
	if err != nil {
		return nil, err
	}
	return &fluentHook{
		Logger: logger,
		levels: defaultLevels,
	}, nil
}
開發者ID:troq,項目名稱:logrus_fluent,代碼行數:13,代碼來源:fluent.go

示例10: New

// New returns initialized logrus hook for fluentd with persistent fluentd logger.
func New(host string, port int) (*FluentHook, error) {
	fd, err := fluent.New(fluent.Config{FluentHost: host, FluentPort: port})
	if err != nil {
		return nil, err
	}

	return &FluentHook{
		levels:       defaultLevels,
		Fluent:       fd,
		ignoreFields: make(map[string]struct{}),
		filters:      make(map[string]func(interface{}) interface{}),
	}, nil
}
開發者ID:evalphobia,項目名稱:logrus_fluent,代碼行數:14,代碼來源:fluent.go

示例11: main

func main() {
	var err error
	logger, err = fluent.New(fluent.Config{
		FluentHost: "localhost",
		FluentPort: 24224,
	})
	if err != nil {
		log.Fatal(err)
	}

	http.HandleFunc("/demo", demoHandler)
	http.ListenAndServe(":8080", nil)
}
開發者ID:GoogleCloudPlatform,項目名稱:golang-samples,代碼行數:13,代碼來源:main.go

示例12: SetupFluentLogger

func SetupFluentLogger(cfg *Config) error {
	if cfg.Fluent.FluentHost == "" {
		logger.Infof(mylog, "The fluentd host is not specified. Skipping fluent logger instantiation.")
		return nil
	}
	logger.Infof(mylog, "Initializing fluent logger based on config: %+v", cfg.Fluent)

	fcli, err := gfluent.New(cfg.Fluent)
	if err != nil {
		return err
	}

	logger.Registry().AddOutput(fluent.FluentLogger{fcli})
	return nil
}
開發者ID:postfix,項目名稱:otaru,代碼行數:15,代碼來源:fluent.go

示例13: print

func print(s severity, args ...interface{}) {
	//connect fluent server
	f, err := fluent.New(fluent.Config{
		FluentPort: 24224,
		FluentHost: "localhost",
		TagPrefix:  "goluent." + getHostname(),
	})

	message := fmt.Sprint(args...)

	stdLog.Println(message)
	if err == nil {
		f.Post(severityName[s], map[string]string{"message": message})
	}
}
開發者ID:nna774,項目名稱:goluent,代碼行數:15,代碼來源:goluent.go

示例14: main

func main() {
	logger, err := fluent.New(fluent.Config{FluentPort: 24224, FluentHost: "127.0.0.1"})
	if err != nil {
		fmt.Println(err)
	}
	defer logger.Close()
	tag := "myapp.access"
	var data = map[string]string{
		"foo":  "bar",
		"hoge": "hoge"}
	i := 0
	for i < 100 {
		logger.Post(tag, data)
		i = i + 1
	}
}
開發者ID:bluele,項目名稱:fluent-logger-golang,代碼行數:16,代碼來源:main.go

示例15: main

func main() {
	lambda_proc.Run(func(context *lambda_proc.Context, eventJSON json.RawMessage) (interface{}, error) {
		fluentURL, err := getfluentURL(context.FunctionName)
		fmt.Fprintf(os.Stderr, "fluentd connection config=%s %d\n", fluentURL.Host, fluentURL.Port)

		logger, err := fluent.New(fluent.Config{FluentPort: fluentURL.Port, FluentHost: fluentURL.Host})

		if err != nil {
			fmt.Fprintf(os.Stderr, "fluentd connection error=%s\n", err)
			return nil, err
		}
		defer logger.Close()

		var event cloudwatchlogs.Event
		err = json.Unmarshal([]byte(eventJSON), &event)
		if err != nil {
			fmt.Fprintf(os.Stderr, "json.Unmarshal err=%s\n", err)
			return nil, err
		}

		d, err := event.AWSLogs.DecodedData()
		if err != nil {
			fmt.Fprintf(os.Stderr, "AWSLogs.DecodedData err=%s\n", err)
			return nil, err
		}

		logs, errs := 0, 0
		for _, e := range d.LogEvents {

			event, err := decodeLogLine(e.Message)
			if err != nil {
				fmt.Fprintf(os.Stderr, "Error decoding log line err=%s\n", err)
				continue
			}

			tag := d.LogGroup

			err = logger.Post(tag, event)
			if err != nil {
				fmt.Fprintf(os.Stderr, "FluentD Post: %s\n", err)
				return nil, err
			}
		}

		return fmt.Sprintf("LogGroup=%s LogStream=%s MessageType=%s NumLogEvents=%d logs=%d errs=%d", d.LogGroup, d.LogStream, d.MessageType, len(d.LogEvents), logs, errs), nil
	})
}
開發者ID:gmelika,項目名稱:rack,代碼行數:47,代碼來源:main.go


注:本文中的github.com/fluent/fluent-logger-golang/fluent.New函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。