0000: 70 61 63 6b 61 67 65 20 64 75 67 6f 6e 67 0a 0a package dugong..
0010: 69 6d 70 6f 72 74 20 28 0a 09 22 63 6f 6d 70 72 import (.."compr
0020: 65 73 73 2f 67 7a 69 70 22 0a 09 22 66 6d 74 22 ess/gzip".."fmt"
0030: 0a 09 6c 6f 67 20 22 67 69 74 68 75 62 2e 63 6f ..log "github.co
0040: 6d 2f 53 69 72 75 70 73 65 6e 2f 6c 6f 67 72 75 m/Sirupsen/logru
0050: 73 22 0a 09 22 69 6f 22 0a 09 22 6f 73 22 0a 09 s".."io".."os"..
0060: 22 70 61 74 68 2f 66 69 6c 65 70 61 74 68 22 0a "path/filepath".
0070: 09 22 73 79 6e 63 2f 61 74 6f 6d 69 63 22 0a 09 ."sync/atomic"..
0080: 22 74 69 6d 65 22 0a 29 0a 0a 2f 2f 20 52 6f 74 "time".)..// Rot
0090: 61 74 69 6f 6e 53 63 68 65 64 75 6c 65 72 20 64 ationScheduler d
00a0: 65 74 65 72 6d 69 6e 65 73 20 77 68 65 6e 20 66 etermines when f
00b0: 69 6c 65 73 20 73 68 6f 75 6c 64 20 62 65 20 72 iles should be r
00c0: 6f 74 61 74 65 64 2e 0a 74 79 70 65 20 52 6f 74 otated..type Rot
00d0: 61 74 69 6f 6e 53 63 68 65 64 75 6c 65 72 20 69 ationScheduler i
00e0: 6e 74 65 72 66 61 63 65 20 7b 0a 09 2f 2f 20 53 nterface {..// S
00f0: 68 6f 75 6c 64 52 6f 74 61 74 65 20 72 65 74 75 houldRotate retu
0100: 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 20 rns true if the
0110: 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 72 file should be r
0120: 6f 74 61 74 65 64 2e 20 54 68 65 20 73 75 66 66 otated. The suff
0130: 69 78 20 74 6f 20 61 70 70 6c 79 0a 09 2f 2f 20 ix to apply..//
0140: 74 6f 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 to the filename
0150: 69 73 20 72 65 74 75 72 6e 65 64 20 61 73 20 74 is returned as t
0160: 68 65 20 32 6e 64 20 61 72 67 2e 0a 09 53 68 6f he 2nd arg...Sho
0170: 75 6c 64 52 6f 74 61 74 65 28 29 20 28 62 6f 6f uldRotate() (boo
0180: 6c 2c 20 73 74 72 69 6e 67 29 0a 09 2f 2f 20 53 l, string)..// S
0190: 68 6f 75 6c 64 47 5a 69 70 20 72 65 74 75 72 6e houldGZip return
01a0: 73 20 74 72 75 65 20 69 66 20 74 68 65 20 66 69 s true if the fi
01b0: 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 67 7a 69 le should be gzi
01c0: 70 70 65 64 20 77 68 65 6e 20 69 74 20 69 73 20 pped when it is
01d0: 72 6f 74 61 74 65 64 2e 0a 09 53 68 6f 75 6c 64 rotated...Should
01e0: 47 5a 69 70 28 29 20 62 6f 6f 6c 0a 7d 0a 0a 2f GZip() bool.}../
01f0: 2f 20 44 61 69 6c 79 52 6f 74 61 74 69 6f 6e 53 / DailyRotationS
0200: 63 68 65 64 75 6c 65 20 72 6f 74 61 74 65 73 20 chedule rotates
0210: 6c 6f 67 20 66 69 6c 65 73 20 64 61 69 6c 79 2e log files daily.
0220: 20 4c 6f 67 73 20 61 72 65 20 6f 6e 6c 79 20 72 Logs are only r
0230: 6f 74 61 74 65 64 0a 2f 2f 20 77 68 65 6e 20 6d otated.// when m
0240: 69 64 6e 69 67 68 74 20 70 61 73 73 65 73 20 2a idnight passes *
0250: 77 68 69 6c 73 74 20 74 68 65 20 70 72 6f 63 65 whilst the proce
0260: 73 73 20 69 73 20 72 75 6e 6e 69 6e 67 2a 2e 20 ss is running*.
0270: 45 2e 67 3a 20 69 66 20 79 6f 75 20 72 75 6e 0a E.g: if you run.
0280: 2f 2f 20 74 68 65 20 70 72 6f 63 65 73 73 20 6f // the process o
0290: 6e 20 44 61 79 20 34 20 74 68 65 6e 20 73 74 6f n Day 4 then sto
02a0: 70 20 69 74 20 61 6e 64 20 73 74 61 72 74 20 69 p it and start i
02b0: 74 20 6f 6e 20 44 61 79 20 37 2c 20 6e 6f 20 72 t on Day 7, no r
02c0: 6f 74 61 74 69 6f 6e 20 77 69 6c 6c 0a 2f 2f 20 otation will.//
02d0: 6f 63 63 75 72 20 77 68 65 6e 20 74 68 65 20 70 occur when the p
02e0: 72 6f 63 65 73 73 20 73 74 61 72 74 73 2e 0a 74 rocess starts..t
02f0: 79 70 65 20 44 61 69 6c 79 52 6f 74 61 74 69 6f ype DailyRotatio
0300: 6e 53 63 68 65 64 75 6c 65 20 73 74 72 75 63 74 nSchedule struct
0310: 20 7b 0a 09 47 5a 69 70 20 20 20 20 20 20 20 20 {..GZip
0320: 62 6f 6f 6c 0a 09 72 6f 74 61 74 65 41 66 74 65 bool..rotateAfte
0330: 72 20 2a 74 69 6d 65 2e 54 69 6d 65 0a 7d 0a 0a r *time.Time.}..
0340: 76 61 72 20 63 75 72 72 65 6e 74 54 69 6d 65 20 var currentTime
0350: 3d 20 74 69 6d 65 2e 4e 6f 77 20 2f 2f 20 65 78 = time.Now // ex
0360: 63 6c 75 73 69 76 65 6c 79 20 66 6f 72 20 74 65 clusively for te
0370: 73 74 69 6e 67 0a 0a 66 75 6e 63 20 64 61 79 4f sting..func dayO
0380: 66 66 73 65 74 28 74 20 74 69 6d 65 2e 54 69 6d ffset(t time.Tim
0390: 65 2c 20 6f 66 66 73 65 74 44 61 79 73 20 69 6e e, offsetDays in
03a0: 74 29 20 74 69 6d 65 2e 54 69 6d 65 20 7b 0a 09 t) time.Time {..
03b0: 2f 2f 20 47 6f 44 6f 63 3a 0a 09 2f 2f 20 20 20 // GoDoc:..//
03c0: 54 68 65 20 6d 6f 6e 74 68 2c 20 64 61 79 2c 20 The month, day,
03d0: 68 6f 75 72 2c 20 6d 69 6e 2c 20 73 65 63 2c 20 hour, min, sec,
03e0: 61 6e 64 20 6e 73 65 63 20 76 61 6c 75 65 73 20 and nsec values
03f0: 6d 61 79 20 62 65 20 6f 75 74 73 69 64 65 20 74 may be outside t
0400: 68 65 69 72 0a 09 2f 2f 20 20 20 75 73 75 61 6c heir..// usual
0410: 20 72 61 6e 67 65 73 20 61 6e 64 20 77 69 6c 6c ranges and will
0420: 20 62 65 20 6e 6f 72 6d 61 6c 69 7a 65 64 20 64 be normalized d
0430: 75 72 69 6e 67 20 74 68 65 20 63 6f 6e 76 65 72 uring the conver
0440: 73 69 6f 6e 2e 0a 09 2f 2f 20 20 20 46 6f 72 20 sion...// For
0450: 65 78 61 6d 70 6c 65 2c 20 4f 63 74 6f 62 65 72 example, October
0460: 20 33 32 20 63 6f 6e 76 65 72 74 73 20 74 6f 20 32 converts to
0470: 4e 6f 76 65 6d 62 65 72 20 31 2e 0a 09 72 65 74 November 1...ret
0480: 75 72 6e 20 74 69 6d 65 2e 44 61 74 65 28 0a 09 urn time.Date(..
0490: 09 74 2e 59 65 61 72 28 29 2c 20 74 2e 4d 6f 6e .t.Year(), t.Mon
04a0: 74 68 28 29 2c 20 74 2e 44 61 79 28 29 2b 6f 66 th(), t.Day()+of
04b0: 66 73 65 74 44 61 79 73 2c 20 30 2c 20 30 2c 20 fsetDays, 0, 0,
04c0: 30 2c 20 30 2c 20 74 2e 4c 6f 63 61 74 69 6f 6e 0, 0, t.Location
04d0: 28 29 2c 0a 09 29 0a 7d 0a 0a 66 75 6e 63 20 28 (),..).}..func (
04e0: 72 73 20 2a 44 61 69 6c 79 52 6f 74 61 74 69 6f rs *DailyRotatio
04f0: 6e 53 63 68 65 64 75 6c 65 29 20 53 68 6f 75 6c nSchedule) Shoul
0500: 64 52 6f 74 61 74 65 28 29 20 28 62 6f 6f 6c 2c dRotate() (bool,
0510: 20 73 74 72 69 6e 67 29 20 7b 0a 09 6e 6f 77 20 string) {..now
0520: 3a 3d 20 63 75 72 72 65 6e 74 54 69 6d 65 28 29 := currentTime()
0530: 0a 09 69 66 20 72 73 2e 72 6f 74 61 74 65 41 66 ..if rs.rotateAf
0540: 74 65 72 20 3d 3d 20 6e 69 6c 20 7b 0a 09 09 6e ter == nil {...n
0550: 65 78 74 52 6f 74 61 74 65 20 3a 3d 20 64 61 79 extRotate := day
0560: 4f 66 66 73 65 74 28 6e 6f 77 2c 20 31 29 0a 09 Offset(now, 1)..
0570: 09 72 73 2e 72 6f 74 61 74 65 41 66 74 65 72 20 .rs.rotateAfter
0580: 3d 20 26 6e 65 78 74 52 6f 74 61 74 65 0a 09 09 = &nextRotate...
0590: 72 65 74 75 72 6e 20 66 61 6c 73 65 2c 20 22 22 return false, ""
05a0: 0a 09 7d 0a 09 69 66 20 6e 6f 77 2e 41 66 74 65 ..}..if now.Afte
05b0: 72 28 2a 72 73 2e 72 6f 74 61 74 65 41 66 74 65 r(*rs.rotateAfte
05c0: 72 29 20 7b 0a 09 09 2f 2f 20 74 68 65 20 73 75 r) {...// the su
05d0: 66 66 69 78 20 73 68 6f 75 6c 64 20 62 65 20 61 ffix should be a
05e0: 63 74 75 61 6c 6c 79 20 74 68 65 20 64 61 74 65 ctually the date
05f0: 20 6f 66 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 of the complete
0600: 20 64 61 79 20 62 65 69 6e 67 20 6c 6f 67 67 65 day being logge
0610: 64 0a 09 09 61 63 74 75 61 6c 44 61 79 20 3a 3d d...actualDay :=
0620: 20 64 61 79 4f 66 66 73 65 74 28 2a 72 73 2e 72 dayOffset(*rs.r
0630: 6f 74 61 74 65 41 66 74 65 72 2c 20 2d 31 29 0a otateAfter, -1).
0640: 09 09 73 75 66 66 69 78 20 3a 3d 20 22 2e 22 20 ..suffix := "."
0650: 2b 20 61 63 74 75 61 6c 44 61 79 2e 46 6f 72 6d + actualDay.Form
0660: 61 74 28 22 32 30 30 36 2d 30 31 2d 30 32 22 29 at("2006-01-02")
0670: 20 2f 2f 20 59 59 59 59 2d 4d 4d 2d 44 44 0a 09 // YYYY-MM-DD..
0680: 09 6e 65 78 74 52 6f 74 61 74 65 20 3a 3d 20 64 .nextRotate := d
0690: 61 79 4f 66 66 73 65 74 28 6e 6f 77 2c 20 31 29 ayOffset(now, 1)
06a0: 0a 09 09 72 73 2e 72 6f 74 61 74 65 41 66 74 65 ...rs.rotateAfte
06b0: 72 20 3d 20 26 6e 65 78 74 52 6f 74 61 74 65 0a r = &nextRotate.
06c0: 09 09 72 65 74 75 72 6e 20 74 72 75 65 2c 20 73 ..return true, s
06d0: 75 66 66 69 78 0a 09 7d 0a 09 72 65 74 75 72 6e uffix..}..return
06e0: 20 66 61 6c 73 65 2c 20 22 22 0a 7d 0a 0a 66 75 false, "".}..fu
06f0: 6e 63 20 28 72 73 20 2a 44 61 69 6c 79 52 6f 74 nc (rs *DailyRot
0700: 61 74 69 6f 6e 53 63 68 65 64 75 6c 65 29 20 53 ationSchedule) S
0710: 68 6f 75 6c 64 47 5a 69 70 28 29 20 62 6f 6f 6c houldGZip() bool
0720: 20 7b 0a 09 72 65 74 75 72 6e 20 72 73 2e 47 5a {..return rs.GZ
0730: 69 70 0a 7d 0a 0a 2f 2f 20 4e 65 77 46 53 48 6f ip.}..// NewFSHo
0740: 6f 6b 20 6d 61 6b 65 73 20 61 20 6c 6f 67 67 69 ok makes a loggi
0750: 6e 67 20 68 6f 6f 6b 20 74 68 61 74 20 77 72 69 ng hook that wri
0760: 74 65 73 20 66 6f 72 6d 61 74 74 65 64 0a 2f 2f tes formatted.//
0770: 20 6c 6f 67 20 65 6e 74 72 69 65 73 20 74 6f 20 log entries to
0780: 69 6e 66 6f 2c 20 77 61 72 6e 20 61 6e 64 20 65 info, warn and e
0790: 72 72 6f 72 20 6c 6f 67 20 66 69 6c 65 73 2e 20 rror log files.
07a0: 45 61 63 68 20 6c 6f 67 20 66 69 6c 65 0a 2f 2f Each log file.//
07b0: 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 65 contains the me
07c0: 73 73 61 67 65 73 20 77 69 74 68 20 74 68 61 74 ssages with that
07d0: 20 73 65 76 65 72 69 74 79 20 6f 72 20 68 69 67 severity or hig
07e0: 68 65 72 2e 20 49 66 20 61 20 66 6f 72 6d 61 74 her. If a format
07f0: 74 65 72 20 69 73 0a 2f 2f 20 6e 6f 74 20 73 70 ter is.// not sp
0800: 65 63 69 66 69 65 64 2c 20 74 68 65 79 20 77 69 ecified, they wi
0810: 6c 6c 20 62 65 20 6c 6f 67 67 65 64 20 75 73 69 ll be logged usi
0820: 6e 67 20 61 20 4a 53 4f 4e 20 66 6f 72 6d 61 74 ng a JSON format
0830: 74 65 72 2e 20 49 66 20 61 0a 2f 2f 20 52 6f 74 ter. If a.// Rot
0840: 61 74 69 6f 6e 53 63 68 65 64 75 6c 65 72 20 69 ationScheduler i
0850: 73 20 73 65 74 2c 20 74 68 65 20 66 69 6c 65 73 s set, the files
0860: 20 77 69 6c 6c 20 62 65 20 63 79 63 6c 65 64 20 will be cycled
0870: 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 69 74 73 according to its
0880: 20 72 75 6c 65 73 2e 0a 66 75 6e 63 20 4e 65 77 rules..func New
0890: 46 53 48 6f 6f 6b 28 69 6e 66 6f 50 61 74 68 2c FSHook(infoPath,
08a0: 20 77 61 72 6e 50 61 74 68 2c 20 65 72 72 6f 72 warnPath, error
08b0: 50 61 74 68 20 73 74 72 69 6e 67 2c 20 66 6f 72 Path string, for
08c0: 6d 61 74 74 65 72 20 6c 6f 67 2e 46 6f 72 6d 61 matter log.Forma
08d0: 74 74 65 72 2c 20 72 6f 74 53 63 68 65 64 20 52 tter, rotSched R
08e0: 6f 74 61 74 69 6f 6e 53 63 68 65 64 75 6c 65 72 otationScheduler
08f0: 29 20 6c 6f 67 2e 48 6f 6f 6b 20 7b 0a 09 69 66 ) log.Hook {..if
0900: 20 66 6f 72 6d 61 74 74 65 72 20 3d 3d 20 6e 69 formatter == ni
0910: 6c 20 7b 0a 09 09 66 6f 72 6d 61 74 74 65 72 20 l {...formatter
0920: 3d 20 26 6c 6f 67 2e 4a 53 4f 4e 46 6f 72 6d 61 = &log.JSONForma
0930: 74 74 65 72 7b 7d 0a 09 7d 0a 09 68 6f 6f 6b 20 tter{}..}..hook
0940: 3a 3d 20 26 66 73 48 6f 6f 6b 7b 0a 09 09 65 6e := &fsHook{...en
0950: 74 72 69 65 73 3a 20 20 20 6d 61 6b 65 28 63 68 tries: make(ch
0960: 61 6e 20 6c 6f 67 2e 45 6e 74 72 79 2c 20 31 30 an log.Entry, 10
0970: 32 34 29 2c 0a 09 09 69 6e 66 6f 50 61 74 68 3a 24),...infoPath:
0980: 20 20 69 6e 66 6f 50 61 74 68 2c 0a 09 09 77 61 infoPath,...wa
0990: 72 6e 50 61 74 68 3a 20 20 77 61 72 6e 50 61 74 rnPath: warnPat
09a0: 68 2c 0a 09 09 65 72 72 6f 72 50 61 74 68 3a 20 h,...errorPath:
09b0: 65 72 72 6f 72 50 61 74 68 2c 0a 09 09 66 6f 72 errorPath,...for
09c0: 6d 61 74 74 65 72 3a 20 66 6f 72 6d 61 74 74 65 matter: formatte
09d0: 72 2c 0a 09 09 73 63 68 65 64 75 6c 65 72 3a 20 r,...scheduler:
09e0: 72 6f 74 53 63 68 65 64 2c 0a 09 7d 0a 0a 09 67 rotSched,..}...g
09f0: 6f 20 66 75 6e 63 28 29 20 7b 0a 09 09 66 6f 72 o func() {...for
0a00: 20 65 6e 74 72 79 20 3a 3d 20 72 61 6e 67 65 20 entry := range
0a10: 68 6f 6f 6b 2e 65 6e 74 72 69 65 73 20 7b 0a 09 hook.entries {..
0a20: 09 09 69 66 20 65 72 72 20 3a 3d 20 68 6f 6f 6b ..if err := hook
0a30: 2e 77 72 69 74 65 45 6e 74 72 79 28 26 65 6e 74 .writeEntry(&ent
0a40: 72 79 29 3b 20 65 72 72 20 21 3d 20 6e 69 6c 20 ry); err != nil
0a50: 7b 0a 09 09 09 09 66 6d 74 2e 46 70 72 69 6e 74 {.....fmt.Fprint
0a60: 66 28 6f 73 2e 53 74 64 65 72 72 2c 20 22 45 72 f(os.Stderr, "Er
0a70: 72 6f 72 20 77 72 69 74 69 6e 67 20 74 6f 20 6c ror writing to l
0a80: 6f 67 66 69 6c 65 3a 20 25 76 5c 6e 22 2c 20 65 ogfile: %v\n", e
0a90: 72 72 29 0a 09 09 09 7d 0a 09 09 09 61 74 6f 6d rr)....}....atom
0aa0: 69 63 2e 41 64 64 49 6e 74 33 32 28 26 68 6f 6f ic.AddInt32(&hoo
0ab0: 6b 2e 71 75 65 75 65 53 69 7a 65 2c 20 2d 31 29 k.queueSize, -1)
0ac0: 0a 09 09 7d 0a 09 7d 28 29 0a 0a 09 72 65 74 75 ...}..}()...retu
0ad0: 72 6e 20 68 6f 6f 6b 0a 7d 0a 0a 74 79 70 65 20 rn hook.}..type
0ae0: 66 73 48 6f 6f 6b 20 73 74 72 75 63 74 20 7b 0a fsHook struct {.
0af0: 09 65 6e 74 72 69 65 73 20 20 20 63 68 61 6e 20 .entries chan
0b00: 6c 6f 67 2e 45 6e 74 72 79 0a 09 71 75 65 75 65 log.Entry..queue
0b10: 53 69 7a 65 20 69 6e 74 33 32 0a 09 69 6e 66 6f Size int32..info
0b20: 50 61 74 68 20 20 73 74 72 69 6e 67 0a 09 77 61 Path string..wa
0b30: 72 6e 50 61 74 68 20 20 73 74 72 69 6e 67 0a 09 rnPath string..
0b40: 65 72 72 6f 72 50 61 74 68 20 73 74 72 69 6e 67 errorPath string
0b50: 0a 09 66 6f 72 6d 61 74 74 65 72 20 6c 6f 67 2e ..formatter log.
0b60: 46 6f 72 6d 61 74 74 65 72 0a 09 73 63 68 65 64 Formatter..sched
0b70: 75 6c 65 72 20 52 6f 74 61 74 69 6f 6e 53 63 68 uler RotationSch
0b80: 65 64 75 6c 65 72 0a 7d 0a 0a 66 75 6e 63 20 28 eduler.}..func (
0b90: 68 6f 6f 6b 20 2a 66 73 48 6f 6f 6b 29 20 46 69 hook *fsHook) Fi
0ba0: 72 65 28 65 6e 74 72 79 20 2a 6c 6f 67 2e 45 6e re(entry *log.En
0bb0: 74 72 79 29 20 65 72 72 6f 72 20 7b 0a 09 61 74 try) error {..at
0bc0: 6f 6d 69 63 2e 41 64 64 49 6e 74 33 32 28 26 68 omic.AddInt32(&h
0bd0: 6f 6f 6b 2e 71 75 65 75 65 53 69 7a 65 2c 20 31 ook.queueSize, 1
0be0: 29 0a 09 68 6f 6f 6b 2e 65 6e 74 72 69 65 73 20 )..hook.entries
0bf0: 3c 2d 20 2a 65 6e 74 72 79 0a 09 72 65 74 75 72 <- *entry..retur
0c00: 6e 20 6e 69 6c 0a 7d 0a 0a 66 75 6e 63 20 28 68 n nil.}..func (h
0c10: 6f 6f 6b 20 2a 66 73 48 6f 6f 6b 29 20 77 72 69 ook *fsHook) wri
0c20: 74 65 45 6e 74 72 79 28 65 6e 74 72 79 20 2a 6c teEntry(entry *l
0c30: 6f 67 2e 45 6e 74 72 79 29 20 65 72 72 6f 72 20 og.Entry) error
0c40: 7b 0a 09 6d 73 67 2c 20 65 72 72 20 3a 3d 20 68 {..msg, err := h
0c50: 6f 6f 6b 2e 66 6f 72 6d 61 74 74 65 72 2e 46 6f ook.formatter.Fo
0c60: 72 6d 61 74 28 65 6e 74 72 79 29 0a 09 69 66 20 rmat(entry)..if
0c70: 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 72 err != nil {...r
0c80: 65 74 75 72 6e 20 6e 69 6c 0a 09 7d 0a 0a 09 69 eturn nil..}...i
0c90: 66 20 68 6f 6f 6b 2e 73 63 68 65 64 75 6c 65 72 f hook.scheduler
0ca0: 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 69 66 20 73 != nil {...if s
0cb0: 68 6f 75 6c 64 2c 20 73 75 66 66 69 78 20 3a 3d hould, suffix :=
0cc0: 20 68 6f 6f 6b 2e 73 63 68 65 64 75 6c 65 72 2e hook.scheduler.
0cd0: 53 68 6f 75 6c 64 52 6f 74 61 74 65 28 29 3b 20 ShouldRotate();
0ce0: 73 68 6f 75 6c 64 20 7b 0a 09 09 09 69 66 20 65 should {....if e
0cf0: 72 72 20 3a 3d 20 68 6f 6f 6b 2e 72 6f 74 61 74 rr := hook.rotat
0d00: 65 28 73 75 66 66 69 78 2c 20 68 6f 6f 6b 2e 73 e(suffix, hook.s
0d10: 63 68 65 64 75 6c 65 72 2e 53 68 6f 75 6c 64 47 cheduler.ShouldG
0d20: 5a 69 70 28 29 29 3b 20 65 72 72 20 21 3d 20 6e Zip()); err != n
0d30: 69 6c 20 7b 0a 09 09 09 09 72 65 74 75 72 6e 20 il {.....return
0d40: 65 72 72 0a 09 09 09 7d 0a 09 09 7d 0a 09 7d 0a err....}...}..}.
0d50: 0a 09 69 66 20 65 6e 74 72 79 2e 4c 65 76 65 6c ..if entry.Level
0d60: 20 3c 3d 20 6c 6f 67 2e 45 72 72 6f 72 4c 65 76 <= log.ErrorLev
0d70: 65 6c 20 7b 0a 09 09 69 66 20 65 72 72 20 3a 3d el {...if err :=
0d80: 20 6c 6f 67 54 6f 46 69 6c 65 28 68 6f 6f 6b 2e logToFile(hook.
0d90: 65 72 72 6f 72 50 61 74 68 2c 20 6d 73 67 29 3b errorPath, msg);
0da0: 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 09 err != nil {...
0db0: 09 72 65 74 75 72 6e 20 65 72 72 0a 09 09 7d 0a .return err...}.
0dc0: 09 7d 0a 0a 09 69 66 20 65 6e 74 72 79 2e 4c 65 .}...if entry.Le
0dd0: 76 65 6c 20 3c 3d 20 6c 6f 67 2e 57 61 72 6e 4c vel <= log.WarnL
0de0: 65 76 65 6c 20 7b 0a 09 09 69 66 20 65 72 72 20 evel {...if err
0df0: 3a 3d 20 6c 6f 67 54 6f 46 69 6c 65 28 68 6f 6f := logToFile(hoo
0e00: 6b 2e 77 61 72 6e 50 61 74 68 2c 20 6d 73 67 29 k.warnPath, msg)
0e10: 3b 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 ; err != nil {..
0e20: 09 09 72 65 74 75 72 6e 20 65 72 72 0a 09 09 7d ..return err...}
0e30: 0a 09 7d 0a 0a 09 69 66 20 65 6e 74 72 79 2e 4c ..}...if entry.L
0e40: 65 76 65 6c 20 3c 3d 20 6c 6f 67 2e 49 6e 66 6f evel <= log.Info
0e50: 4c 65 76 65 6c 20 7b 0a 09 09 69 66 20 65 72 72 Level {...if err
0e60: 20 3a 3d 20 6c 6f 67 54 6f 46 69 6c 65 28 68 6f := logToFile(ho
0e70: 6f 6b 2e 69 6e 66 6f 50 61 74 68 2c 20 6d 73 67 ok.infoPath, msg
0e80: 29 3b 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a ); err != nil {.
0e90: 09 09 09 72 65 74 75 72 6e 20 65 72 72 0a 09 09 ...return err...
0ea0: 7d 0a 09 7d 0a 0a 09 72 65 74 75 72 6e 20 6e 69 }..}...return ni
0eb0: 6c 0a 7d 0a 0a 66 75 6e 63 20 28 68 6f 6f 6b 20 l.}..func (hook
0ec0: 2a 66 73 48 6f 6f 6b 29 20 4c 65 76 65 6c 73 28 *fsHook) Levels(
0ed0: 29 20 5b 5d 6c 6f 67 2e 4c 65 76 65 6c 20 7b 0a ) []log.Level {.
0ee0: 09 72 65 74 75 72 6e 20 5b 5d 6c 6f 67 2e 4c 65 .return []log.Le
0ef0: 76 65 6c 7b 0a 09 09 6c 6f 67 2e 50 61 6e 69 63 vel{...log.Panic
0f00: 4c 65 76 65 6c 2c 0a 09 09 6c 6f 67 2e 46 61 74 Level,...log.Fat
0f10: 61 6c 4c 65 76 65 6c 2c 0a 09 09 6c 6f 67 2e 45 alLevel,...log.E
0f20: 72 72 6f 72 4c 65 76 65 6c 2c 0a 09 09 6c 6f 67 rrorLevel,...log
0f30: 2e 57 61 72 6e 4c 65 76 65 6c 2c 0a 09 09 6c 6f .WarnLevel,...lo
0f40: 67 2e 49 6e 66 6f 4c 65 76 65 6c 2c 0a 09 7d 0a g.InfoLevel,..}.
0f50: 7d 0a 0a 2f 2f 20 72 6f 74 61 74 65 20 61 6c 6c }..// rotate all
0f60: 20 74 68 65 20 6c 6f 67 20 66 69 6c 65 73 20 74 the log files t
0f70: 6f 20 74 68 65 20 67 69 76 65 6e 20 73 75 66 66 o the given suff
0f80: 69 78 2e 0a 2f 2f 20 49 66 20 65 72 72 6f 72 20 ix..// If error
0f90: 70 61 74 68 20 69 73 20 22 65 72 72 2e 6c 6f 67 path is "err.log
0fa0: 22 20 61 6e 64 20 73 75 66 66 69 78 20 69 73 20 " and suffix is
0fb0: 22 31 22 20 74 68 65 6e 20 6d 6f 76 65 0a 2f 2f "1" then move.//
0fc0: 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 74 6f the contents to
0fd0: 20 22 65 72 72 2e 6c 6f 67 31 22 2e 0a 2f 2f 20 "err.log1"..//
0fe0: 54 68 69 73 20 72 65 71 75 69 72 65 73 20 6e 6f This requires no
0ff0: 20 6c 6f 63 6b 69 6e 67 20 61 73 20 74 68 65 20 locking as the
1000: 67 6f 72 6f 75 74 69 6e 65 20 63 61 6c 6c 69 6e goroutine callin
1010: 67 20 74 68 69 73 20 69 73 20 74 68 65 20 73 61 g this is the sa
1020: 6d 65 0a 2f 2f 20 6f 6e 65 20 77 68 69 63 68 20 me.// one which
1030: 64 6f 65 73 20 74 68 65 20 6c 6f 67 67 69 6e 67 does the logging
1040: 2e 20 53 69 6e 63 65 20 77 65 20 64 6f 6e 27 74 . Since we don't
1050: 20 68 6f 6c 64 20 6f 70 65 6e 20 61 20 68 61 6e hold open a han
1060: 64 6c 65 20 74 6f 20 74 68 65 0a 2f 2f 20 66 69 dle to the.// fi
1070: 6c 65 20 77 68 65 6e 20 77 72 69 74 69 6e 67 2c le when writing,
1080: 20 61 20 73 69 6d 70 6c 65 20 52 65 6e 61 6d 65 a simple Rename
1090: 20 69 73 20 61 6c 6c 20 74 68 61 74 20 69 73 20 is all that is
10a0: 72 65 71 75 69 72 65 64 2e 0a 66 75 6e 63 20 28 required..func (
10b0: 68 6f 6f 6b 20 2a 66 73 48 6f 6f 6b 29 20 72 6f hook *fsHook) ro
10c0: 74 61 74 65 28 73 75 66 66 69 78 20 73 74 72 69 tate(suffix stri
10d0: 6e 67 2c 20 67 7a 69 70 20 62 6f 6f 6c 29 20 65 ng, gzip bool) e
10e0: 72 72 6f 72 20 7b 0a 09 66 6f 72 20 5f 2c 20 66 rror {..for _, f
10f0: 70 61 74 68 20 3a 3d 20 72 61 6e 67 65 20 5b 5d path := range []
1100: 73 74 72 69 6e 67 7b 68 6f 6f 6b 2e 65 72 72 6f string{hook.erro
1110: 72 50 61 74 68 2c 20 68 6f 6f 6b 2e 77 61 72 6e rPath, hook.warn
1120: 50 61 74 68 2c 20 68 6f 6f 6b 2e 69 6e 66 6f 50 Path, hook.infoP
1130: 61 74 68 7d 20 7b 0a 09 09 6c 6f 67 46 69 6c 65 ath} {...logFile
1140: 50 61 74 68 20 3a 3d 20 66 70 61 74 68 20 2b 20 Path := fpath +
1150: 73 75 66 66 69 78 0a 09 09 69 66 20 65 72 72 20 suffix...if err
1160: 3a 3d 20 6f 73 2e 52 65 6e 61 6d 65 28 66 70 61 := os.Rename(fpa
1170: 74 68 2c 20 6c 6f 67 46 69 6c 65 50 61 74 68 29 th, logFilePath)
1180: 3b 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 ; err != nil {..
1190: 09 09 2f 2f 20 65 2e 67 2e 20 62 65 63 61 75 73 ..// e.g. becaus
11a0: 65 20 74 68 65 72 65 20 77 65 72 65 20 6e 6f 20 e there were no
11b0: 65 72 72 6f 72 73 20 69 6e 20 65 72 72 6f 72 2e errors in error.
11c0: 6c 6f 67 20 66 6f 72 20 74 68 69 73 20 64 61 79 log for this day
11d0: 0a 09 09 09 66 6d 74 2e 46 70 72 69 6e 74 66 28 ....fmt.Fprintf(
11e0: 6f 73 2e 53 74 64 65 72 72 2c 20 22 45 72 72 6f os.Stderr, "Erro
11f0: 72 20 72 6f 74 61 74 69 6e 67 20 66 69 6c 65 20 r rotating file
1200: 25 73 3a 20 25 76 5c 6e 22 2c 20 66 70 61 74 68 %s: %v\n", fpath
1210: 2c 20 65 72 72 29 0a 09 09 09 63 6f 6e 74 69 6e , err)....contin
1220: 75 65 20 2f 2f 20 64 6f 6e 27 74 20 74 72 79 20 ue // don't try
1230: 74 6f 20 67 7a 69 70 20 69 66 20 77 65 20 66 61 to gzip if we fa
1240: 69 6c 65 64 20 74 6f 20 72 6f 74 61 74 65 0a 09 iled to rotate..
1250: 09 7d 0a 09 09 69 66 20 67 7a 69 70 20 7b 0a 09 .}...if gzip {..
1260: 09 09 69 66 20 65 72 72 20 3a 3d 20 67 7a 69 70 ..if err := gzip
1270: 46 69 6c 65 28 6c 6f 67 46 69 6c 65 50 61 74 68 File(logFilePath
1280: 29 3b 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a ); err != nil {.
1290: 09 09 09 09 66 6d 74 2e 46 70 72 69 6e 74 66 28 ....fmt.Fprintf(
12a0: 6f 73 2e 53 74 64 65 72 72 2c 20 22 46 61 69 6c os.Stderr, "Fail
12b0: 65 64 20 74 6f 20 67 7a 69 70 20 66 69 6c 65 20 ed to gzip file
12c0: 25 73 3a 20 25 76 5c 6e 22 2c 20 6c 6f 67 46 69 %s: %v\n", logFi
12d0: 6c 65 50 61 74 68 2c 20 65 72 72 29 0a 09 09 09 lePath, err)....
12e0: 7d 0a 09 09 7d 0a 09 7d 0a 09 72 65 74 75 72 6e }...}..}..return
12f0: 20 6e 69 6c 0a 7d 0a 0a 66 75 6e 63 20 6c 6f 67 nil.}..func log
1300: 54 6f 46 69 6c 65 28 70 61 74 68 20 73 74 72 69 ToFile(path stri
1310: 6e 67 2c 20 6d 73 67 20 5b 5d 62 79 74 65 29 20 ng, msg []byte)
1320: 65 72 72 6f 72 20 7b 0a 09 66 64 2c 20 65 72 72 error {..fd, err
1330: 20 3a 3d 20 6f 73 2e 4f 70 65 6e 46 69 6c 65 28 := os.OpenFile(
1340: 70 61 74 68 2c 20 6f 73 2e 4f 5f 57 52 4f 4e 4c path, os.O_WRONL
1350: 59 7c 6f 73 2e 4f 5f 41 50 50 45 4e 44 7c 6f 73 Y|os.O_APPEND|os
1360: 2e 4f 5f 43 52 45 41 54 45 2c 20 30 36 30 30 29 .O_CREATE, 0600)
1370: 0a 09 69 66 20 65 72 72 20 21 3d 20 6e 69 6c 20 ..if err != nil
1380: 7b 0a 09 09 72 65 74 75 72 6e 20 65 72 72 0a 09 {...return err..
1390: 7d 0a 09 64 65 66 65 72 20 66 64 2e 43 6c 6f 73 }..defer fd.Clos
13a0: 65 28 29 0a 09 5f 2c 20 65 72 72 20 3d 20 66 64 e().._, err = fd
13b0: 2e 57 72 69 74 65 28 6d 73 67 29 0a 09 72 65 74 .Write(msg)..ret
13c0: 75 72 6e 20 65 72 72 0a 7d 0a 0a 66 75 6e 63 20 urn err.}..func
13d0: 67 7a 69 70 46 69 6c 65 28 66 70 61 74 68 20 73 gzipFile(fpath s
13e0: 74 72 69 6e 67 29 20 65 72 72 6f 72 20 7b 0a 09 tring) error {..
13f0: 72 65 61 64 65 72 2c 20 65 72 72 20 3a 3d 20 6f reader, err := o
1400: 73 2e 4f 70 65 6e 28 66 70 61 74 68 29 0a 09 69 s.Open(fpath)..i
1410: 66 20 65 72 72 20 21 3d 20 6e 69 6c 20 7b 0a 09 f err != nil {..
1420: 09 72 65 74 75 72 6e 20 65 72 72 0a 09 7d 0a 0a .return err..}..
1430: 09 66 69 6c 65 6e 61 6d 65 20 3a 3d 20 66 69 6c .filename := fil
1440: 65 70 61 74 68 2e 42 61 73 65 28 66 70 61 74 68 epath.Base(fpath
1450: 29 0a 09 74 61 72 67 65 74 20 3a 3d 20 66 69 6c )..target := fil
1460: 65 70 61 74 68 2e 4a 6f 69 6e 28 66 69 6c 65 70 epath.Join(filep
1470: 61 74 68 2e 44 69 72 28 66 70 61 74 68 29 2c 20 ath.Dir(fpath),
1480: 66 69 6c 65 6e 61 6d 65 2b 22 2e 67 7a 22 29 0a filename+".gz").
1490: 09 77 72 69 74 65 72 2c 20 65 72 72 20 3a 3d 20 .writer, err :=
14a0: 6f 73 2e 43 72 65 61 74 65 28 74 61 72 67 65 74 os.Create(target
14b0: 29 0a 09 69 66 20 65 72 72 20 21 3d 20 6e 69 6c )..if err != nil
14c0: 20 7b 0a 09 09 72 65 74 75 72 6e 20 65 72 72 0a {...return err.
14d0: 09 7d 0a 09 64 65 66 65 72 20 77 72 69 74 65 72 .}..defer writer
14e0: 2e 43 6c 6f 73 65 28 29 0a 0a 09 61 72 63 68 69 .Close()...archi
14f0: 76 65 72 20 3a 3d 20 67 7a 69 70 2e 4e 65 77 57 ver := gzip.NewW
1500: 72 69 74 65 72 28 77 72 69 74 65 72 29 0a 09 61 riter(writer)..a
1510: 72 63 68 69 76 65 72 2e 4e 61 6d 65 20 3d 20 66 rchiver.Name = f
1520: 69 6c 65 6e 61 6d 65 0a 09 64 65 66 65 72 20 61 ilename..defer a
1530: 72 63 68 69 76 65 72 2e 43 6c 6f 73 65 28 29 0a rchiver.Close().
1540: 0a 09 5f 2c 20 65 72 72 20 3d 20 69 6f 2e 43 6f .._, err = io.Co
1550: 70 79 28 61 72 63 68 69 76 65 72 2c 20 72 65 61 py(archiver, rea
1560: 64 65 72 29 0a 09 72 65 74 75 72 6e 20 65 72 72 der)..return err
1570: 0a 7d 0a .}.