Moved some logging code from datamananager to loggerutil.
[arvados.git] / services / datamanager / loggerutil / loggerutil.go
1 /* Datamanager-specific logging methods. */
2
3 package loggerutil
4
5 import (
6         "git.curoverse.com/arvados.git/sdk/go/logger"
7         "log"
8         "os"
9         "runtime"
10         "time"
11 )
12
13 // Useful to call at the begining of execution to log info about the
14 // current run.
15 func LogRunInfo(arvLogger *logger.Logger) {
16         if arvLogger != nil {
17                 now := time.Now()
18                 arvLogger.Update(func(p map[string]interface{}, e map[string]interface{}) {
19                         runInfo := make(map[string]interface{})
20                         runInfo["time_started"] = now
21                         runInfo["args"] = os.Args
22                         hostname, err := os.Hostname()
23                         if err != nil {
24                                 runInfo["hostname_error"] = err.Error()
25                         } else {
26                                 runInfo["hostname"] = hostname
27                         }
28                         runInfo["pid"] = os.Getpid()
29                         p["run_info"] = runInfo
30                 })
31         }
32 }
33
34 // A LogMutator that records the current memory usage. This is most useful as a logger write hook.
35 //
36 // Assumes we already have a map named "run_info" in properties. LogRunInfo() can create such a map for you if you call it.
37 func LogMemoryAlloc(p map[string]interface{}, e map[string]interface{}) {
38         runInfo := p["run_info"].(map[string]interface{})
39         var memStats runtime.MemStats
40         runtime.ReadMemStats(&memStats)
41         runInfo["alloc_bytes_in_use"] = memStats.Alloc
42 }
43
44 func FatalWithMessage(arvLogger *logger.Logger, message string) {
45         if arvLogger != nil {
46                 arvLogger.FinalUpdate(func(p map[string]interface{}, e map[string]interface{}) {
47                         p["FATAL"] = message
48                         p["run_info"].(map[string]interface{})["time_finished"] = time.Now()
49                 })
50         }
51
52         log.Fatalf(message)
53 }