github.com/kat-co/cmd@v0.0.0-20140616103059-5da365f9d57e/logging_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cmd_test 5 6 import ( 7 "io/ioutil" 8 "path/filepath" 9 10 "github.com/juju/loggo" 11 "github.com/juju/testing" 12 gc "launchpad.net/gocheck" 13 "github.com/juju/cmd/cmdtesting" 14 15 "github.com/juju/cmd" 16 ) 17 18 var logger = loggo.GetLogger("juju.test") 19 20 type LogSuite struct { 21 testing.CleanupSuite 22 } 23 24 var _ = gc.Suite(&LogSuite{}) 25 26 func (s *LogSuite) SetUpTest(c *gc.C) { 27 s.CleanupSuite.SetUpTest(c) 28 s.AddCleanup(func(_ *gc.C) { 29 loggo.ResetLoggers() 30 loggo.ResetWriters() 31 }) 32 } 33 34 func newLogWithFlags(c *gc.C, defaultConfig string, flags ...string) *cmd.Log { 35 log := &cmd.Log{ 36 DefaultConfig: defaultConfig, 37 } 38 flagSet := cmdtesting.NewFlagSet() 39 log.AddFlags(flagSet) 40 err := flagSet.Parse(false, flags) 41 c.Assert(err, gc.IsNil) 42 return log 43 } 44 45 func (s *LogSuite) TestNoFlags(c *gc.C) { 46 log := newLogWithFlags(c, "") 47 c.Assert(log.Path, gc.Equals, "") 48 c.Assert(log.Quiet, gc.Equals, false) 49 c.Assert(log.Verbose, gc.Equals, false) 50 c.Assert(log.Debug, gc.Equals, false) 51 c.Assert(log.Config, gc.Equals, "") 52 } 53 54 func (s *LogSuite) TestFlags(c *gc.C) { 55 log := newLogWithFlags(c, "", "--log-file", "foo", "--verbose", "--debug", 56 "--logging-config=juju.cmd=INFO;juju.worker.deployer=DEBUG") 57 c.Assert(log.Path, gc.Equals, "foo") 58 c.Assert(log.Verbose, gc.Equals, true) 59 c.Assert(log.Debug, gc.Equals, true) 60 c.Assert(log.Config, gc.Equals, "juju.cmd=INFO;juju.worker.deployer=DEBUG") 61 } 62 63 func (s *LogSuite) TestLogConfigFromDefault(c *gc.C) { 64 config := "juju.cmd=INFO;juju.worker.deployer=DEBUG" 65 log := newLogWithFlags(c, config) 66 log.DefaultConfig = config 67 c.Assert(log.Path, gc.Equals, "") 68 c.Assert(log.Verbose, gc.Equals, false) 69 c.Assert(log.Debug, gc.Equals, false) 70 c.Assert(log.Config, gc.Equals, config) 71 } 72 73 func (s *LogSuite) TestDebugSetsLogLevel(c *gc.C) { 74 l := &cmd.Log{Debug: true} 75 ctx := cmdtesting.Context(c) 76 err := l.Start(ctx) 77 c.Assert(err, gc.IsNil) 78 79 c.Assert(loggo.GetLogger("").LogLevel(), gc.Equals, loggo.DEBUG) 80 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "") 81 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "") 82 } 83 84 func (s *LogSuite) TestShowLogSetsLogLevel(c *gc.C) { 85 l := &cmd.Log{ShowLog: true} 86 ctx := cmdtesting.Context(c) 87 err := l.Start(ctx) 88 c.Assert(err, gc.IsNil) 89 90 c.Assert(loggo.GetLogger("").LogLevel(), gc.Equals, loggo.INFO) 91 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "") 92 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "") 93 } 94 95 func (s *LogSuite) TestStderr(c *gc.C) { 96 l := &cmd.Log{ShowLog: true, Config: "<root>=INFO"} 97 ctx := cmdtesting.Context(c) 98 err := l.Start(ctx) 99 c.Assert(err, gc.IsNil) 100 logger.Infof("hello") 101 c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.* INFO .* hello\n`) 102 } 103 104 func (s *LogSuite) TestRelPathLog(c *gc.C) { 105 l := &cmd.Log{Path: "foo.log", Config: "<root>=INFO"} 106 ctx := cmdtesting.Context(c) 107 err := l.Start(ctx) 108 c.Assert(err, gc.IsNil) 109 logger.Infof("hello") 110 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log")) 111 c.Assert(err, gc.IsNil) 112 c.Assert(string(content), gc.Matches, `^.* INFO .* hello\n`) 113 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "") 114 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "") 115 } 116 117 func (s *LogSuite) TestAbsPathLog(c *gc.C) { 118 path := filepath.Join(c.MkDir(), "foo.log") 119 l := &cmd.Log{Path: path, Config: "<root>=INFO"} 120 ctx := cmdtesting.Context(c) 121 err := l.Start(ctx) 122 c.Assert(err, gc.IsNil) 123 logger.Infof("hello") 124 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "") 125 content, err := ioutil.ReadFile(path) 126 c.Assert(err, gc.IsNil) 127 c.Assert(string(content), gc.Matches, `^.* INFO .* hello\n`) 128 } 129 130 func (s *LogSuite) TestLoggingToFileAndStderr(c *gc.C) { 131 l := &cmd.Log{Path: "foo.log", Config: "<root>=INFO", ShowLog: true} 132 ctx := cmdtesting.Context(c) 133 err := l.Start(ctx) 134 c.Assert(err, gc.IsNil) 135 logger.Infof("hello") 136 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log")) 137 c.Assert(err, gc.IsNil) 138 c.Assert(string(content), gc.Matches, `^.* INFO .* hello\n`) 139 c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.* INFO .* hello\n`) 140 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "") 141 } 142 143 func (s *LogSuite) TestErrorAndWarningLoggingToStderr(c *gc.C) { 144 // Error and warning go to stderr even with ShowLog=false 145 l := &cmd.Log{Config: "<root>=INFO", ShowLog: false} 146 ctx := cmdtesting.Context(c) 147 err := l.Start(ctx) 148 c.Assert(err, gc.IsNil) 149 logger.Warningf("a warning") 150 logger.Errorf("an error") 151 logger.Infof("an info") 152 c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.*WARNING a warning\n.*ERROR an error\n.*`) 153 c.Assert(cmdtesting.Stdout(ctx), gc.Equals, "") 154 } 155 156 func (s *LogSuite) TestQuietAndVerbose(c *gc.C) { 157 l := &cmd.Log{Verbose: true, Quiet: true} 158 ctx := cmdtesting.Context(c) 159 err := l.Start(ctx) 160 c.Assert(err, gc.ErrorMatches, `"verbose" and "quiet" flags clash, please use one or the other, not both`) 161 } 162 163 func (s *LogSuite) TestOutputDefault(c *gc.C) { 164 l := &cmd.Log{} 165 ctx := cmdtesting.Context(c) 166 err := l.Start(ctx) 167 c.Assert(err, gc.IsNil) 168 169 ctx.Infof("Writing info output") 170 ctx.Verbosef("Writing verbose output") 171 172 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Writing info output\n") 173 } 174 175 func (s *LogSuite) TestOutputVerbose(c *gc.C) { 176 l := &cmd.Log{Verbose: true} 177 ctx := cmdtesting.Context(c) 178 err := l.Start(ctx) 179 c.Assert(err, gc.IsNil) 180 181 ctx.Infof("Writing info output") 182 ctx.Verbosef("Writing verbose output") 183 184 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Writing info output\nWriting verbose output\n") 185 } 186 187 func (s *LogSuite) TestOutputQuiet(c *gc.C) { 188 l := &cmd.Log{Quiet: true} 189 ctx := cmdtesting.Context(c) 190 err := l.Start(ctx) 191 c.Assert(err, gc.IsNil) 192 193 ctx.Infof("Writing info output") 194 ctx.Verbosef("Writing verbose output") 195 196 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "") 197 } 198 199 func (s *LogSuite) TestOutputQuietLogs(c *gc.C) { 200 l := &cmd.Log{Quiet: true, Path: "foo.log", Config: "<root>=INFO"} 201 ctx := cmdtesting.Context(c) 202 err := l.Start(ctx) 203 c.Assert(err, gc.IsNil) 204 205 ctx.Infof("Writing info output") 206 ctx.Verbosef("Writing verbose output") 207 208 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log")) 209 c.Assert(err, gc.IsNil) 210 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "") 211 c.Assert(string(content), gc.Matches, `^.*INFO .* Writing info output\n.*INFO .*Writing verbose output\n.*`) 212 } 213 214 func (s *LogSuite) TestOutputDefaultLogsVerbose(c *gc.C) { 215 l := &cmd.Log{Path: "foo.log", Config: "<root>=INFO"} 216 ctx := cmdtesting.Context(c) 217 err := l.Start(ctx) 218 c.Assert(err, gc.IsNil) 219 220 ctx.Infof("Writing info output") 221 ctx.Verbosef("Writing verbose output") 222 223 content, err := ioutil.ReadFile(filepath.Join(ctx.Dir, "foo.log")) 224 c.Assert(err, gc.IsNil) 225 c.Assert(cmdtesting.Stderr(ctx), gc.Equals, "Writing info output\n") 226 c.Assert(string(content), gc.Matches, `^.*INFO .*Writing verbose output\n.*`) 227 } 228 229 func (s *LogSuite) TestOutputDebugForcesQuiet(c *gc.C) { 230 l := &cmd.Log{Verbose: true, Debug: true} 231 ctx := cmdtesting.Context(c) 232 err := l.Start(ctx) 233 c.Assert(err, gc.IsNil) 234 235 ctx.Infof("Writing info output") 236 ctx.Verbosef("Writing verbose output") 237 238 c.Assert(cmdtesting.Stderr(ctx), gc.Matches, `^.*INFO .* Writing info output\n.*INFO .*Writing verbose output\n.*`) 239 }