code.gitea.io/gitea@v1.21.7/cmd/manager_logging.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package cmd 5 6 import ( 7 "fmt" 8 "os" 9 10 "code.gitea.io/gitea/modules/log" 11 "code.gitea.io/gitea/modules/private" 12 13 "github.com/urfave/cli/v2" 14 ) 15 16 var ( 17 defaultLoggingFlags = []cli.Flag{ 18 &cli.StringFlag{ 19 Name: "logger", 20 Usage: `Logger name - will default to "default"`, 21 }, 22 &cli.StringFlag{ 23 Name: "writer", 24 Usage: "Name of the log writer - will default to mode", 25 }, 26 &cli.StringFlag{ 27 Name: "level", 28 Usage: "Logging level for the new logger", 29 }, 30 &cli.StringFlag{ 31 Name: "stacktrace-level", 32 Aliases: []string{"L"}, 33 Usage: "Stacktrace logging level", 34 }, 35 &cli.StringFlag{ 36 Name: "flags", 37 Aliases: []string{"F"}, 38 Usage: "Flags for the logger", 39 }, 40 &cli.StringFlag{ 41 Name: "expression", 42 Aliases: []string{"e"}, 43 Usage: "Matching expression for the logger", 44 }, 45 &cli.StringFlag{ 46 Name: "prefix", 47 Aliases: []string{"p"}, 48 Usage: "Prefix for the logger", 49 }, 50 &cli.BoolFlag{ 51 Name: "color", 52 Usage: "Use color in the logs", 53 }, 54 &cli.BoolFlag{ 55 Name: "debug", 56 }, 57 } 58 59 subcmdLogging = &cli.Command{ 60 Name: "logging", 61 Usage: "Adjust logging commands", 62 Subcommands: []*cli.Command{ 63 { 64 Name: "pause", 65 Usage: "Pause logging (Gitea will buffer logs up to a certain point and will drop them after that point)", 66 Flags: []cli.Flag{ 67 &cli.BoolFlag{ 68 Name: "debug", 69 }, 70 }, 71 Action: runPauseLogging, 72 }, { 73 Name: "resume", 74 Usage: "Resume logging", 75 Flags: []cli.Flag{ 76 &cli.BoolFlag{ 77 Name: "debug", 78 }, 79 }, 80 Action: runResumeLogging, 81 }, { 82 Name: "release-and-reopen", 83 Usage: "Cause Gitea to release and re-open files used for logging", 84 Flags: []cli.Flag{ 85 &cli.BoolFlag{ 86 Name: "debug", 87 }, 88 }, 89 Action: runReleaseReopenLogging, 90 }, { 91 Name: "remove", 92 Usage: "Remove a logger", 93 ArgsUsage: "[name] Name of logger to remove", 94 Flags: []cli.Flag{ 95 &cli.BoolFlag{ 96 Name: "debug", 97 }, &cli.StringFlag{ 98 Name: "logger", 99 Usage: `Logger name - will default to "default"`, 100 }, 101 }, 102 Action: runRemoveLogger, 103 }, { 104 Name: "add", 105 Usage: "Add a logger", 106 Subcommands: []*cli.Command{ 107 { 108 Name: "file", 109 Usage: "Add a file logger", 110 Flags: append(defaultLoggingFlags, []cli.Flag{ 111 &cli.StringFlag{ 112 Name: "filename", 113 Aliases: []string{"f"}, 114 Usage: "Filename for the logger - this must be set.", 115 }, 116 &cli.BoolFlag{ 117 Name: "rotate", 118 Aliases: []string{"r"}, 119 Usage: "Rotate logs", 120 Value: true, 121 }, 122 &cli.Int64Flag{ 123 Name: "max-size", 124 Aliases: []string{"s"}, 125 Usage: "Maximum size in bytes before rotation", 126 }, 127 &cli.BoolFlag{ 128 Name: "daily", 129 Aliases: []string{"d"}, 130 Usage: "Rotate logs daily", 131 Value: true, 132 }, 133 &cli.IntFlag{ 134 Name: "max-days", 135 Aliases: []string{"D"}, 136 Usage: "Maximum number of daily logs to keep", 137 }, 138 &cli.BoolFlag{ 139 Name: "compress", 140 Aliases: []string{"z"}, 141 Usage: "Compress rotated logs", 142 Value: true, 143 }, 144 &cli.IntFlag{ 145 Name: "compression-level", 146 Aliases: []string{"Z"}, 147 Usage: "Compression level to use", 148 }, 149 }...), 150 Action: runAddFileLogger, 151 }, { 152 Name: "conn", 153 Usage: "Add a net conn logger", 154 Flags: append(defaultLoggingFlags, []cli.Flag{ 155 &cli.BoolFlag{ 156 Name: "reconnect-on-message", 157 Aliases: []string{"R"}, 158 Usage: "Reconnect to host for every message", 159 }, 160 &cli.BoolFlag{ 161 Name: "reconnect", 162 Aliases: []string{"r"}, 163 Usage: "Reconnect to host when connection is dropped", 164 }, 165 &cli.StringFlag{ 166 Name: "protocol", 167 Aliases: []string{"P"}, 168 Usage: "Set protocol to use: tcp, unix, or udp (defaults to tcp)", 169 }, 170 &cli.StringFlag{ 171 Name: "address", 172 Aliases: []string{"a"}, 173 Usage: "Host address and port to connect to (defaults to :7020)", 174 }, 175 }...), 176 Action: runAddConnLogger, 177 }, 178 }, 179 }, { 180 Name: "log-sql", 181 Usage: "Set LogSQL", 182 Flags: []cli.Flag{ 183 &cli.BoolFlag{ 184 Name: "debug", 185 }, 186 &cli.BoolFlag{ 187 Name: "off", 188 Usage: "Switch off SQL logging", 189 }, 190 }, 191 Action: runSetLogSQL, 192 }, 193 }, 194 } 195 ) 196 197 func runRemoveLogger(c *cli.Context) error { 198 ctx, cancel := installSignals() 199 defer cancel() 200 201 setup(ctx, c.Bool("debug")) 202 logger := c.String("logger") 203 if len(logger) == 0 { 204 logger = log.DEFAULT 205 } 206 writer := c.Args().First() 207 208 extra := private.RemoveLogger(ctx, logger, writer) 209 return handleCliResponseExtra(extra) 210 } 211 212 func runAddConnLogger(c *cli.Context) error { 213 ctx, cancel := installSignals() 214 defer cancel() 215 216 setup(ctx, c.Bool("debug")) 217 vals := map[string]any{} 218 mode := "conn" 219 vals["net"] = "tcp" 220 if c.IsSet("protocol") { 221 switch c.String("protocol") { 222 case "udp": 223 vals["net"] = "udp" 224 case "unix": 225 vals["net"] = "unix" 226 } 227 } 228 if c.IsSet("address") { 229 vals["address"] = c.String("address") 230 } else { 231 vals["address"] = ":7020" 232 } 233 if c.IsSet("reconnect") { 234 vals["reconnect"] = c.Bool("reconnect") 235 } 236 if c.IsSet("reconnect-on-message") { 237 vals["reconnectOnMsg"] = c.Bool("reconnect-on-message") 238 } 239 return commonAddLogger(c, mode, vals) 240 } 241 242 func runAddFileLogger(c *cli.Context) error { 243 ctx, cancel := installSignals() 244 defer cancel() 245 246 setup(ctx, c.Bool("debug")) 247 vals := map[string]any{} 248 mode := "file" 249 if c.IsSet("filename") { 250 vals["filename"] = c.String("filename") 251 } else { 252 return fmt.Errorf("filename must be set when creating a file logger") 253 } 254 if c.IsSet("rotate") { 255 vals["rotate"] = c.Bool("rotate") 256 } 257 if c.IsSet("max-size") { 258 vals["maxsize"] = c.Int64("max-size") 259 } 260 if c.IsSet("daily") { 261 vals["daily"] = c.Bool("daily") 262 } 263 if c.IsSet("max-days") { 264 vals["maxdays"] = c.Int("max-days") 265 } 266 if c.IsSet("compress") { 267 vals["compress"] = c.Bool("compress") 268 } 269 if c.IsSet("compression-level") { 270 vals["compressionLevel"] = c.Int("compression-level") 271 } 272 return commonAddLogger(c, mode, vals) 273 } 274 275 func commonAddLogger(c *cli.Context, mode string, vals map[string]any) error { 276 if len(c.String("level")) > 0 { 277 vals["level"] = log.LevelFromString(c.String("level")).String() 278 } 279 if len(c.String("stacktrace-level")) > 0 { 280 vals["stacktraceLevel"] = log.LevelFromString(c.String("stacktrace-level")).String() 281 } 282 if len(c.String("expression")) > 0 { 283 vals["expression"] = c.String("expression") 284 } 285 if len(c.String("prefix")) > 0 { 286 vals["prefix"] = c.String("prefix") 287 } 288 if len(c.String("flags")) > 0 { 289 vals["flags"] = log.FlagsFromString(c.String("flags")) 290 } 291 if c.IsSet("color") { 292 vals["colorize"] = c.Bool("color") 293 } 294 logger := log.DEFAULT 295 if c.IsSet("logger") { 296 logger = c.String("logger") 297 } 298 writer := mode 299 if c.IsSet("writer") { 300 writer = c.String("writer") 301 } 302 ctx, cancel := installSignals() 303 defer cancel() 304 305 extra := private.AddLogger(ctx, logger, writer, mode, vals) 306 return handleCliResponseExtra(extra) 307 } 308 309 func runPauseLogging(c *cli.Context) error { 310 ctx, cancel := installSignals() 311 defer cancel() 312 313 setup(ctx, c.Bool("debug")) 314 userMsg := private.PauseLogging(ctx) 315 _, _ = fmt.Fprintln(os.Stdout, userMsg) 316 return nil 317 } 318 319 func runResumeLogging(c *cli.Context) error { 320 ctx, cancel := installSignals() 321 defer cancel() 322 323 setup(ctx, c.Bool("debug")) 324 userMsg := private.ResumeLogging(ctx) 325 _, _ = fmt.Fprintln(os.Stdout, userMsg) 326 return nil 327 } 328 329 func runReleaseReopenLogging(c *cli.Context) error { 330 ctx, cancel := installSignals() 331 defer cancel() 332 333 setup(ctx, c.Bool("debug")) 334 userMsg := private.ReleaseReopenLogging(ctx) 335 _, _ = fmt.Fprintln(os.Stdout, userMsg) 336 return nil 337 } 338 339 func runSetLogSQL(c *cli.Context) error { 340 ctx, cancel := installSignals() 341 defer cancel() 342 setup(ctx, c.Bool("debug")) 343 344 extra := private.SetLogSQL(ctx, !c.Bool("off")) 345 return handleCliResponseExtra(extra) 346 }