func newWriter(addr string) (io.Writer, error) { u, err := url.Parse(addr) if u.Scheme != "syslog" { return nil, fmt.Errorf("unsupported scheme '%v' currently supported only 'syslog'", u.Scheme) } pr, err := parseSyslogPriority(u) if err != nil { return nil, err } var w io.Writer if u.Host != "" { w, err = syslog.Dial("udp", u.Host, pr, SyslogTag) } else if u.Path != "" { w, err = syslog.Dial("unixgram", u.Path, pr, SyslogTag) } else if u.Host == "" && u.Path == "" { w, err = syslog.Dial("", "", pr, SyslogTag) } else { return nil, fmt.Errorf("unsupported address format: %v", addr) } if err != nil { return nil, err } return &prefixWriter{p: []byte(parsePrefix(u)), w: w}, nil }
// StatsAndLogging constructs a Statter and an AuditLogger based on its config // parameters, and return them both. Crashes if any setup fails. // Also sets the constructed AuditLogger as the default logger, and configures // the cfssl, mysql, and grpc packages to use our logger. // This must be called before any gRPC code is called, because gRPC's SetLogger // doesn't use any locking. func StatsAndLogging(statConf StatsdConfig, logConf SyslogConfig) (metrics.Statter, blog.Logger) { stats, err := metrics.NewStatter(statConf.Server, statConf.Prefix) FailOnError(err, "Couldn't connect to statsd") tag := path.Base(os.Args[0]) syslogger, err := syslog.Dial( "", "", syslog.LOG_INFO, // default, not actually used tag) FailOnError(err, "Could not connect to Syslog") syslogLevel := int(syslog.LOG_INFO) if logConf.SyslogLevel != 0 { syslogLevel = logConf.SyslogLevel } logger, err := blog.New(syslogger, logConf.StdoutLevel, syslogLevel) FailOnError(err, "Could not connect to Syslog") _ = blog.Set(logger) cfsslLog.SetLogger(cfsslLogger{logger}) _ = mysql.SetLogger(mysqlLogger{logger}) grpclog.SetLogger(grpcLogger{logger}) return stats, logger }
// Dial establishes a connection to the log daemon by passing through // the parameters to the syslog.Dial method. // See http://golang.org/pkg/log/syslog/#Dial func Dial(network, raddr string, tag string, stats statsd.Statter) (*AuditLogger, error) { syslogger, err := syslog.Dial(network, raddr, syslog.LOG_INFO|syslog.LOG_LOCAL0, tag) if err != nil { return nil, err } return NewAuditLogger(syslogger, stats) }
// initSyslog creates a connection to syslog and stores the handler in ctx func initSyslog(orig_logctx Logging) (logctx Logging, err error) { defer func() { if e := recover(); e != nil { err = fmt.Errorf("mig.initSyslog() -> %v", e) } }() logctx = orig_logctx if logctx.Host == "" { panic("Syslog host is missing") } if logctx.Port < 1 { panic("Syslog port is missing") } if logctx.Protocol == "" { panic("Syslog protocol is missing") } dialaddr := logctx.Host + ":" + fmt.Sprintf("%d", logctx.Port) logctx.syslogfd, err = syslog.Dial(logctx.Protocol, dialaddr, syslog.LOG_LOCAL3|syslog.LOG_INFO, "mig_scheduler") if err != nil { panic(err) } if err != nil { panic(err) } return }
func DialSyslogLogger(network, address, tag string, priority Priority) (Logger, error) { if wrt, err := syslog.Dial(network, address, syslogPriority(priority), tag); err == nil { return &SyslogLogger{writer: wrt}, nil } else { return nil, err } }
func New(ctx logger.Context) (logger.Logger, error) { tag := ctx.Config["syslog-tag"] if tag == "" { tag = ctx.ContainerID[:12] } proto, address, err := parseAddress(ctx.Config["syslog-address"]) if err != nil { return nil, err } facility, err := parseFacility(ctx.Config["syslog-facility"]) if err != nil { return nil, err } log, err := syslog.Dial( proto, address, facility, path.Base(os.Args[0])+"/"+tag, ) if err != nil { return nil, err } return &Syslog{ writer: log, }, nil }
// StatsAndLogging constructs a Statter and an AuditLogger based on its config // parameters, and return them both. Crashes if any setup fails. // Also sets the constructed AuditLogger as the default logger. func StatsAndLogging(statConf StatsdConfig, logConf SyslogConfig) (metrics.Statter, blog.Logger) { stats, err := metrics.NewStatter(statConf.Server, statConf.Prefix) FailOnError(err, "Couldn't connect to statsd") tag := path.Base(os.Args[0]) syslogger, err := syslog.Dial( "", "", syslog.LOG_INFO|syslog.LOG_LOCAL0, // default, overridden by log calls tag) FailOnError(err, "Could not connect to Syslog") stdoutLoglevel := int(syslog.LOG_DEBUG) if logConf.StdoutLevel != nil { stdoutLoglevel = *logConf.StdoutLevel } syslogLogLevel := int(syslog.LOG_DEBUG) if logConf.SyslogLevel != nil { syslogLogLevel = *logConf.SyslogLevel } logger, err := blog.New(syslogger, stdoutLoglevel, syslogLogLevel) FailOnError(err, "Could not connect to Syslog") _ = blog.Set(logger) cfsslLog.SetLogger(cfsslLogger{logger}) _ = mysql.SetLogger(mysqlLogger{logger}) return stats, logger }
// StatsAndLogging constructs a Statter and and AuditLogger based on its config // parameters, and return them both. Crashes if any setup fails. // Also sets the constructed AuditLogger as the default logger. func StatsAndLogging(statConf StatsdConfig, logConf SyslogConfig) (statsd.Statter, *blog.AuditLogger) { stats, err := statsd.NewClient(statConf.Server, statConf.Prefix) FailOnError(err, "Couldn't connect to statsd") tag := path.Base(os.Args[0]) syslogger, err := syslog.Dial( logConf.Network, logConf.Server, syslog.LOG_INFO|syslog.LOG_LOCAL0, // default, overridden by log calls tag) FailOnError(err, "Could not connect to Syslog") level := int(syslog.LOG_DEBUG) if logConf.StdoutLevel != nil { level = *logConf.StdoutLevel } auditlogger, err := blog.NewAuditLogger(syslogger, stats, level) FailOnError(err, "Could not connect to Syslog") // TODO(https://github.com/cloudflare/cfssl/issues/426): // CFSSL's log facility always prints to stdout. Ideally we should send a // patch that would allow us to have CFSSL use our log facility. In the // meantime, inhibit debug and info-level logs from CFSSL. cfsslLog.Level = cfsslLog.LevelWarning blog.SetAuditLogger(auditlogger) return stats, auditlogger }
func main() { //l3, err := syslog.New(syslog.LOG_ERR, "GoExample") l, err := syslog.Dial("tcp", "202.207.211.187:5140", syslog.LOG_ERR, "test perf") // connection to a log daemon defer l.Close() if err != nil { fmt.Print(err) log.Fatal("error") } //l3.Emerg("emergency") //l3.Alert("alert") //l3.Crit("critical") //l3.Err("error") //l3.Warning("warning") //l3.Notice("notice") for i := 0; i < 100000; i++ { s := fmt.Sprintf("%d 180.76.15.34 - - [28/Jun/2015:10:49:43 +0800] \"GET / HTTP/1.1\" 200 7783 \"-\" \"Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)\"\n", uint64(i)) l.Info(s) if i%1000 == 0 { t := time.Now() fmt.Printf("%02d:%02d:%02d:%d\n", t.Hour(), t.Minute(), t.Second(), uint64(i)) } time.Sleep(200000) } //l3.Debug("debug") //l3.Write([]byte("write")) }
func main() { address := flag.String("addr", ":5514", "syslog server") level := flag.String("level", "info", "syslog level") message := flag.String("msg", "", "syslog message") flag.Parse() logWriter, err := syslog.Dial("udp", *address, syslog.LOG_ERR, "logger") defer logWriter.Close() if err != nil { log.Fatal("error") } switch *level { case "emerg": logWriter.Emerg(*message) case "alert": logWriter.Alert(*message) case "crit": logWriter.Crit(*message) case "err": logWriter.Err(*message) case "warning": logWriter.Warning(*message) case "notice": logWriter.Notice(*message) case "info": logWriter.Info(*message) case "debug": logWriter.Debug(*message) } }
// New creates a syslog logger using the configuration passed in on // the context. Supported context configuration variables are // syslog-address, syslog-facility, & syslog-tag. func New(ctx logger.Context) (logger.Logger, error) { tag, err := loggerutils.ParseLogTag(ctx, "{{.ID}}") if err != nil { return nil, err } proto, address, err := parseAddress(ctx.Config["syslog-address"]) if err != nil { return nil, err } facility, err := parseFacility(ctx.Config["syslog-facility"]) if err != nil { return nil, err } log, err := syslog.Dial( proto, address, facility, path.Base(os.Args[0])+"/"+tag, ) if err != nil { return nil, err } return &syslogger{ writer: log, }, nil }
func CreateMetrics(appName string, log logging.Logger, outputMetrics bool) (*kodingmetrics.Metrics, *kodingmetrics.DogStatsD) { metric := kodingmetrics.New(appName) // if outputMetrics, do print output to the console if outputMetrics { // change those loggers // https://github.com/rcrowley/go-metrics/blob/37df06ff62a7d8b4473b48d355008c838da87561/log.go // get those numbers from config // output metrics every 1 minutes go metrics.Log(metric.Registry, 6e10, slog.New(os.Stderr, "metrics: ", slog.Lmicroseconds)) } // Left here for future reference // for Mac syslogPath := "/var/run/syslog" if runtime.GOOS != "darwin" { // for linux syslogPath = "/dev/log" } w, err := syslog.Dial("unixgram", syslogPath, syslog.LOG_INFO, "socialapi-metrics") if err != nil { log.Error("Err while initing syslog for metrics, metrics wont be in the syslog %s", err.Error()) } else { go metrics.Syslog(metric.Registry, 30e10, w) } statsd, err := kodingmetrics.NewDogStatsD(appName) if err == nil { go kodingmetrics.Collect(metric.Registry, statsd, 24e10) } return metric, statsd }
func InitDBLog() error { var err error if SysLog, err = syslog.Dial("udp", SLOG_ADDR+":"+SLOG_PORT, syslog.LOG_NOTICE, "goDB"); err != nil { return err } return nil }
func (s *syslogWriter) Open(name string) error { w, err := syslog.Dial(s.scheme, s.addr, syslog.LOG_LOCAL0|syslog.LOG_NOTICE, name) if err != nil { return err } s.w = w return nil }
func createLogger() *log.Logger { syslogWriter, err := syslog.Dial("udp", global.Config.SysLogIP, syslog.LOG_SYSLOG, "sorcery") if err != nil { panic(err) } return log.New(syslogWriter, "EventSauce: ", log.Lshortfile) }
// establishes the connection to syslog. func (conf *syslogModuleConfig) connectToSyslog( network, raddr string, facility int, tag string) error { facilityName, err := FacilityValueToName(facility) if err != nil { return err } var priority goSyslog.Priority = goSyslog.Priority(facility<<3) | goSyslog.LOG_INFO conf.network = network conf.raddr = raddr conf.facility = facility conf.tag = tag conf.syslogConn, err = goSyslog.Dial(network, raddr, priority, tag) if err != nil { log.Printf("Could not open connection to syslog, reason: " + err.Error()) return err } if conf.syslogConn == nil { log.Printf("Could not retrieve connection to syslog") return fmt.Errorf("Could not retrieve connection to syslog") } conf.syslogConn.Debug( fmt.Sprintf( "rlog syslog (re)connected with facility=%d(%s), tag=\"%s\"", facility, facilityName, tag)) conf.syslogConn.Debug( fmt.Sprintf( "rlog syslog network=\"%s\", raddr=\"%s\", heartBeatFilePath=\"%s\"", network, raddr, conf.heartBeatFilePath)) // FIX: heartbeat for debugging only. if conf.heartBeatFilePath != "" { parentDir, _ := filepath.Split(conf.heartBeatFilePath) if parentDir != "" { var dirMode os.FileMode = 0775 // user/group-only read/write/traverse, world read/traverse err = os.MkdirAll(parentDir, dirMode) if err != nil { return err } } err = conf.writeHeartBeat("Starting heartbeat...", true) if err != nil { return err } } return nil }
func syslogStreamer(logline *defines.Log, addr string) error { tag := fmt.Sprintf("%s.%s", logline.Name, logline.Tag) remote, err := syslog.Dial("udp", addr, syslog.LOG_USER|syslog.LOG_INFO, tag) if err != nil { return err } io.WriteString(remote, logline.Data) return nil }
// NewSyslogWriter returns a writer ready to be used with output modules. // If network and address are empty, Dial will connect to the local syslog server. func NewSyslogWriter(network, address string, prio syslog.Priority, tag string) io.Writer { s, err := syslog.Dial(network, address, prio, tag) if err != nil { m := "syslog dial error: " + err.Error() critialLogger(m) panic(m) } return s }
func ExampleDial() { sysLog, err := syslog.Dial("tcp", "localhost:1234", syslog.LOG_WARNING|syslog.LOG_DAEMON, "demotag") if err != nil { log.Fatal(err) } fmt.Fprintf(sysLog, "This is a daemon warning with demotag.") sysLog.Emerg("And this is a daemon emergency with demotag.") }
func NewSyslogdLogger(tag, proto, addr string) LoggerInterface { logger, err := syslog.Dial(proto, addr, syslog.LOG_ERR|syslog.LOG_INFO, tag) if err != nil { panic("Can't init SyslogD connection") } log.SetOutput(logger) log.SetPrefix(tag) return &SyslogdLogger{logger: logger} }
func New(network, raddr string, priority syslog.Priority, tag string) *Handler { w, err := syslog.Dial(network, raddr, priority, tag) if err != nil { log.Fatalf("%v", err) } return &Handler{ w: w, } }
// Configure logger to write to the syslog. func InitLoger(SyslogServer string, AlertCounter int, AlertTime int) { //logwriter, e = syslog.New(syslog.LOG_NOTICE, "IpsecDiagTool") logwriter, e := syslog.Dial("udp", SyslogServer, syslog.LOG_ERR, "IpsecDiagTool") if e == nil && logwriter != nil { logwriter.Info("IpsecDiagTool started!") } logging = Logging{logwriter, AlertCounter, AlertTime} }
func newSyslogHandler(protocol, ipaddr string, priority syslog.Priority, tag string) (sh *SyslogHandler, err error) { sh = &SyslogHandler{} sh.Out, err = syslog.Dial(protocol, ipaddr, priority, tag) if err != nil { return nil, err } return sh, nil }
// NewLogger creates a new Logger func NewLogger(network, addr string, level int, clk clock.Clock) *Logger { if level == 0 { level = 7 } syslogger, err := syslog.Dial(network, addr, defaultPriority, "stapled") if err != nil { panic(err) } return &Logger{syslogger, level, clk} }
func (this *Server) StartDrainer(address string, filter EntryFilter) *Drainer { c := make(chan Entry) listener := &Listener{ Channel: c, Filter: filter, LastEntry: time.Now(), } this.AddListener <- listener drainer := &Drainer{ Address: address, Filter: filter, server: this, listener: listener, terminate: make(chan bool, 1), } go func() { var w *syslog.Writer var err error for entry := range Throttle(c, 100) { for { // If we terminated give up select { case <-drainer.terminate: return default: } // Connect if w == nil { log.Printf("connecting to syslog://%v\n", address) w, err = syslog.Dial("tcp", address, syslog.LOG_INFO, "") if err != nil { w = nil time.Sleep(time.Second * 5) continue } } // Send the message _, err = w.Write(entry.Line()) if err != nil { w.Close() w = nil time.Sleep(time.Second * 5) continue } // Successfully sent the message so break break } } }() return drainer }
func setup(t *testing.T) *AuditLogger { // Write all logs to UDP on a high port so as to not bother the system // which is running the test, particularly for Emerg() writer, err := syslog.Dial("udp", "127.0.0.1:65530", syslog.LOG_INFO|syslog.LOG_LOCAL0, "") test.AssertNotError(t, err, "Could not construct syslog object") stats, _ := statsd.NewNoopClient(nil) audit, err := NewAuditLogger(writer, stats, stdoutLevel) test.AssertNotError(t, err, "Could not construct syslog object") return audit }
// Create a new SyslogBackend instance. func NewSyslogBackend(network, raddr, prefix string) (Backend, error) { if prefix == "" { prefix = "poller" } writer, err := syslog.Dial(network, raddr, syslog.LOG_INFO, prefix) if err != nil { return nil, err } return &syslogBackend{writer: writer}, nil }
// New creates a syslog logger whose default severity level is INFO. func New(network, raddr string, priority syslog.Priority, app string, lfmt gol.LogFormatter) (l gol.Logger) { if w, err := syslog.Dial(network, raddr, syslog.LOG_INFO, app); err != nil { fmt.Printf("syslog.Dial() failed: %s\n", err) } else { l = &Logger{ Logger: simple.Logger{}, writer: w, } l.SetFormatter(lfmt) } return l }
func TestTransmission(t *testing.T) { t.Parallel() l, err := newUDPListener("127.0.0.1:0") test.AssertNotError(t, err, "Failed to open log server") defer l.Close() stats, _ := statsd.NewNoopClient(nil) fmt.Printf("Going to %s\n", l.LocalAddr().String()) writer, err := syslog.Dial("udp", l.LocalAddr().String(), syslog.LOG_INFO|syslog.LOG_LOCAL0, "") test.AssertNotError(t, err, "Failed to find connect to log server") audit, err := NewAuditLogger(writer, stats) test.AssertNotError(t, err, "Failed to construct audit logger") data := make([]byte, 128) audit.Audit("audit-logger_test.go: audit-notice") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Crit("audit-logger_test.go: critical") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Debug("audit-logger_test.go: debug") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Emerg("audit-logger_test.go: emerg") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Err("audit-logger_test.go: err") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Info("audit-logger_test.go: info") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Notice("audit-logger_test.go: notice") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Warning("audit-logger_test.go: warning") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") audit.Alert("audit-logger_test.go: alert") _, _, err = l.ReadFrom(data) test.AssertNotError(t, err, "Failed to find packet") }
func newSyslogPusher(syslogNetwork string, syslogAddress string, logAppName string) (lion.Pusher, error) { writer, err := syslog.Dial( syslogNetwork, syslogAddress, syslog.LOG_INFO, logAppName, ) if err != nil { return nil, err } return sysloglion.NewPusher(writer), nil }