mirror of
				https://github.com/linuxkit/linuxkit.git
				synced 2025-10-31 03:40:16 +00:00 
			
		
		
		
	moby: Create custom formatter for Info() log events
This formatter strips the prefix from Info() events to make the default output of "moby build" more readable. Signed-off-by: Rolf Neugebauer <rolf.neugebauer@docker.com>
This commit is contained in:
		| @@ -8,6 +8,22 @@ import ( | |||||||
| 	log "github.com/Sirupsen/logrus" | 	log "github.com/Sirupsen/logrus" | ||||||
| ) | ) | ||||||
|  |  | ||||||
|  | var ( | ||||||
|  | 	defaultLogFormatter = &log.TextFormatter{} | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | // infoFormatter overrides the default format for Info() log events to | ||||||
|  | // provide an easier to read output | ||||||
|  | type infoFormatter struct { | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (f *infoFormatter) Format(entry *log.Entry) ([]byte, error) { | ||||||
|  | 	if entry.Level == log.InfoLevel { | ||||||
|  | 		return append([]byte(entry.Message), '\n'), nil | ||||||
|  | 	} | ||||||
|  | 	return defaultLogFormatter.Format(entry) | ||||||
|  | } | ||||||
|  |  | ||||||
| func main() { | func main() { | ||||||
| 	flag.Usage = func() { | 	flag.Usage = func() { | ||||||
| 		fmt.Printf("USAGE: %s [options] COMMAND\n\n", os.Args[0]) | 		fmt.Printf("USAGE: %s [options] COMMAND\n\n", os.Args[0]) | ||||||
| @@ -52,7 +68,7 @@ func main() { | |||||||
| 	runDisk := runCmd.String("disk", "", "Path to disk image to used") | 	runDisk := runCmd.String("disk", "", "Path to disk image to used") | ||||||
|  |  | ||||||
| 	// Set up logging | 	// Set up logging | ||||||
| 	log.SetFormatter(&log.TextFormatter{}) | 	log.SetFormatter(new(infoFormatter)) | ||||||
| 	log.SetLevel(log.InfoLevel) | 	log.SetLevel(log.InfoLevel) | ||||||
| 	flag.Parse() | 	flag.Parse() | ||||||
| 	if *flagQuiet && *flagVerbose { | 	if *flagQuiet && *flagVerbose { | ||||||
| @@ -63,6 +79,8 @@ func main() { | |||||||
| 		log.SetLevel(log.ErrorLevel) | 		log.SetLevel(log.ErrorLevel) | ||||||
| 	} | 	} | ||||||
| 	if *flagVerbose { | 	if *flagVerbose { | ||||||
|  | 		// Switch back to the standard formatter | ||||||
|  | 		log.SetFormatter(defaultLogFormatter) | ||||||
| 		log.SetLevel(log.DebugLevel) | 		log.SetLevel(log.DebugLevel) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user