Esempio n. 1
0
func (logger *ConsoleLogger) log() {

	var record *Record
	var file *os.File
	var items []interface{}
	var status string
	var write bool

	for {
		select {
		case record = <-logger.receiver:
			items = record.Items
			write = true
			for _, filter := range ConsoleFilters {
				write, data := filter(record)
				if !write {
					break
				}
				if data != nil {
					items = data
					break
				}
			}
			if !write {
				continue
			}
			argLength := len(items)
			if record.Error {
				file = os.Stderr
			} else {
				file = os.Stdout
			}
			if record.Error {
				status = "ERR"
			} else {
				status = "INF"
			}
			fmt.Fprintf(file, "%s [%s-%s-%s %s:%s:%s]", status,
				encoding.PadInt64(UTC.Year, 4), encoding.PadInt(UTC.Month, 2),
				encoding.PadInt(UTC.Day, 2), encoding.PadInt(UTC.Hour, 2),
				encoding.PadInt(UTC.Minute, 2), encoding.PadInt(UTC.Second, 2))
			for i := 0; i < argLength; i++ {
				fmt.Fprintf(file, " %v", items[i])
			}
			file.Write([]byte("\n"))
		case <-checker:
			if len(logger.receiver) > 0 {
				checker <- 1
				continue
			}
			waiter <- 1
		}
	}

}
Esempio n. 2
0
func (logger *FileLogger) GetFilename(timestamp *time.Time) string {
	var suffix string
	switch logger.rotate {
	case RotateNever:
		suffix = ""
	case RotateDaily:
		suffix = "." + encoding.PadInt64(timestamp.Year, 4) + "-" +
			encoding.PadInt(timestamp.Month, 2) + "-" + encoding.PadInt(timestamp.Day, 2)
	case RotateHourly:
		suffix = "." + encoding.PadInt64(timestamp.Year, 4) + "-" +
			encoding.PadInt(timestamp.Month, 2) + "-" + encoding.PadInt(timestamp.Day, 2) + "." +
			encoding.PadInt(timestamp.Hour, 2)
	case RotateTest:
		suffix = "." + encoding.PadInt64(timestamp.Year, 4) + "-" +
			encoding.PadInt(timestamp.Month, 2) + "-" + encoding.PadInt(timestamp.Day, 2) + "." +
			encoding.PadInt(timestamp.Hour, 2) + "-" + encoding.PadInt(timestamp.Minute, 2) + "-" +
			encoding.PadInt(timestamp.Second, 2)
	}
	filename := logger.name + suffix + ".log"
	return path.Join(logger.directory, filename)
}