Beispiel #1
0
// NewRotatableLogger creates a rotatable file logger with buffering and a priority filter
// Note: if path is empty, logs will be written to stdout
func NewRotatableLogger(path, format string, fields []string, logLevel int, verbose bool) (*Logger, error) {
	var sinks []log.Sink
	if len(path) > 0 {
		rfw, err := NewRotatableFileWriter(
			path,
			os.O_CREATE|os.O_APPEND|os.O_WRONLY,
			os.FileMode(0644),
			5*time.Second)
		if err != nil {
			return nil, err
		}

		sinks = []log.Sink{
			NewCloseablePriorityFilter(
				log.Priority(logLevel),
				NewCloseableBufferedWriterSink(1<<8, rfw, format, fields),
			),
		}
	} else {
		sinks = []log.Sink{
			log.PriorityFilter(
				log.Priority(logLevel),
				log.WriterSink(os.Stdout, format, fields),
			),
		}
	}

	return NewLogger("", verbose, sinks...), nil
}
Beispiel #2
0
func init() {

	Logger = log.NewSimple(log.WriterSink(os.Stdout, "%s: %s[%d] %s\n",
		[]string{"priority", "executable", "pid", "message"}))

	if interfaces, err := net.Interfaces(); err == nil {
		for _, i := range interfaces {
			if i.Flags&net.FlagLoopback == 0 && len(i.HardwareAddr) > 0 {
				hardwareAddr = i.HardwareAddr
				break
			}
		}
	}
	if hardwareAddr == nil {
		// If we failed to obtain the MAC address of the current computer,
		// we will use a randomly generated 6 byte sequence instead and set
		// the multicast bit as recommended in RFC 4122.
		hardwareAddr = make([]byte, 6)
		_, err := io.ReadFull(rand.Reader, hardwareAddr)
		if err != nil {
			panic(err)
		}
		hardwareAddr[0] = hardwareAddr[0] | 0x01
	}

	// initialize the clock sequence with a random number
	var clockSeqRand [2]byte
	io.ReadFull(rand.Reader, clockSeqRand[:])
	clockSeq = uint32(clockSeqRand[1])<<8 | uint32(clockSeqRand[0])
}
Beispiel #3
0
func NewCloseableBufferedWriterSink(bufSize int, out io.WriteCloser, format string, fields []string) SinkCloser {
	buffer := bufio.NewWriterSize(out, bufSize)

	bws := &CloseableBufferedWriterSink{
		CloseableWriterSink: CloseableWriterSink{
			Sink: log.WriterSink(buffer, format, fields),
			out:  out,
		},
		buffer:    buffer,
		nextFlush: time.Now().Add(1 * time.Second).UnixNano(),
	}

	go bws.autoFlush()

	return bws
}
Beispiel #4
0
func setLogger(priority log.Priority) {
	logger = log.NewSimple(
		log.PriorityFilter(
			priority,
			log.WriterSink(os.Stdout, log.BasicFormat, log.BasicFields)))
}
Beispiel #5
0
func NewCloseableWriterSink(out io.WriteCloser, format string, fields []string) SinkCloser {
	return &CloseableWriterSink{
		Sink: log.WriterSink(out, format, fields),
		out:  out,
	}
}
Beispiel #6
0
func init() {
	defaultLogger = NewLogger("", true, clog.WriterSink(os.Stdout, "%s [%s]: %s\n", []string{"priority", "full_time", "message"}))
}