mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-27 04:15:55 +03:00
Modify Startup Output (#2469)
* Move SiteOutput to a seperate function. * Simplify as all sites in Server use the same port * Ensure -quiet supresses fmt.Println calls * Prevent double output of siteinfo to log - improve log message * Use caddy.LogDestination to setup log * Ensure Log is still output if quiet.
This commit is contained in:
parent
396d8e989f
commit
c0190a3460
3 changed files with 31 additions and 10 deletions
3
caddy.go
3
caddy.go
|
@ -74,6 +74,9 @@ var (
|
|||
// false after that.
|
||||
started bool
|
||||
|
||||
// record where the process log is being sent
|
||||
LogDestination string
|
||||
|
||||
// mu protects the variables 'isUpgrade' and 'started'.
|
||||
mu sync.Mutex
|
||||
)
|
||||
|
|
|
@ -58,7 +58,7 @@ func init() {
|
|||
flag.BoolVar(&plugins, "plugins", false, "List installed plugins")
|
||||
flag.StringVar(&certmagic.Email, "email", "", "Default ACME CA account email address")
|
||||
flag.DurationVar(&certmagic.HTTPTimeout, "catimeout", certmagic.HTTPTimeout, "Default ACME CA HTTP timeout")
|
||||
flag.StringVar(&logfile, "log", "", "Process log file")
|
||||
flag.StringVar(&caddy.LogDestination, "log", "", "Process log file")
|
||||
flag.IntVar(&logRollMB, "log-roll-mb", 100, "Roll process log when it reaches this many megabytes (0 to disable rolling)")
|
||||
flag.BoolVar(&logRollCompress, "log-roll-compress", true, "Gzip-compress rolled process log files")
|
||||
flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file")
|
||||
|
@ -82,7 +82,7 @@ func Run() {
|
|||
certmagic.UserAgent = appName + "/" + appVersion
|
||||
|
||||
// Set up process log before anything bad happens
|
||||
switch logfile {
|
||||
switch caddy.LogDestination {
|
||||
case "stdout":
|
||||
log.SetOutput(os.Stdout)
|
||||
case "stderr":
|
||||
|
@ -92,18 +92,18 @@ func Run() {
|
|||
default:
|
||||
if logRollMB > 0 {
|
||||
log.SetOutput(&lumberjack.Logger{
|
||||
Filename: logfile,
|
||||
Filename: caddy.LogDestination,
|
||||
MaxSize: logRollMB,
|
||||
MaxAge: 14,
|
||||
MaxBackups: 10,
|
||||
Compress: logRollCompress,
|
||||
})
|
||||
} else {
|
||||
err := os.MkdirAll(filepath.Dir(logfile), 0755)
|
||||
err := os.MkdirAll(filepath.Dir(caddy.LogDestination), 0755)
|
||||
if err != nil {
|
||||
mustLogFatalf("%v", err)
|
||||
}
|
||||
f, err := os.OpenFile(logfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
f, err := os.OpenFile(caddy.LogDestination, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||
if err != nil {
|
||||
mustLogFatalf("%v", err)
|
||||
}
|
||||
|
@ -573,7 +573,6 @@ var (
|
|||
cpu string
|
||||
envFile string
|
||||
fromJSON bool
|
||||
logfile string
|
||||
logRollMB int
|
||||
logRollCompress bool
|
||||
revoke string
|
||||
|
|
|
@ -507,16 +507,35 @@ func (s *Server) Stop() error {
|
|||
// OnStartupComplete lists the sites served by this server
|
||||
// and any relevant information, assuming caddy.Quiet == false.
|
||||
func (s *Server) OnStartupComplete() {
|
||||
if caddy.Quiet {
|
||||
return
|
||||
if !caddy.Quiet {
|
||||
firstSite := s.sites[0]
|
||||
scheme := "HTTP"
|
||||
if firstSite.TLS.Enabled {
|
||||
scheme = "HTTPS"
|
||||
}
|
||||
fmt.Println("")
|
||||
fmt.Printf("Serving %s on port "+firstSite.Port()+" \n", scheme)
|
||||
s.OutputSiteInfo("fmt")
|
||||
}
|
||||
// if caddy process log is going to stdout, printing to log
|
||||
// here would duplicate to stdout so dont. If -quiet ensure
|
||||
// that Log is still output even if process log startup isnt. #2469
|
||||
if caddy.LogDestination != "stdout" || caddy.Quiet {
|
||||
s.OutputSiteInfo("log")
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) OutputSiteInfo(LogType string) {
|
||||
for _, site := range s.sites {
|
||||
output := site.Addr.String()
|
||||
if caddy.IsLoopback(s.Address()) && !caddy.IsLoopback(site.Addr.Host) {
|
||||
output += " (only accessible on this machine)"
|
||||
}
|
||||
fmt.Println(output)
|
||||
log.Println(output)
|
||||
if LogType == "fmt" {
|
||||
fmt.Println(output)
|
||||
} else {
|
||||
log.Printf("[INFO] Serving %s \n", output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue