View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.start;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.io.PrintStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.nio.file.StandardOpenOption;
27  import java.util.Date;
28  
29  import org.eclipse.jetty.start.config.CommandLineConfigSource;
30  
31  /**
32   * Centralized Place for logging.
33   * <p>
34   * Because startup cannot rely on Jetty's Logging, an alternative logging is established.
35   * <p>
36   * Optional behavior is to create a ${jetty.base}/logs/start.log with whatever output the startup process produces.
37   */
38  public class StartLog
39  {
40      private final static PrintStream stdout = System.out;
41      private final static PrintStream stderr = System.err;
42      private static volatile PrintStream out = System.out;
43      private static volatile PrintStream err = System.err;
44      private final static StartLog INSTANCE = new StartLog();
45  
46      public static void debug(String format, Object... args)
47      {
48          if (INSTANCE.debug)
49          {
50              out.printf(format + "%n",args);
51          }
52      }
53      
54      public static void trace(String format, Object... args)
55      {
56          if (INSTANCE.trace)
57          {
58              out.printf("TRACE: " + format + "%n",args);
59          }
60      }
61  
62      public static void debug(Throwable t)
63      {
64          if (INSTANCE.debug)
65          {
66              t.printStackTrace(out);
67          }
68      }
69  
70      public static StartLog getInstance()
71      {
72          return INSTANCE;
73      }
74      
75      public static void log(String type, String msg)
76      {
77          err.println(type + ": " + msg);
78      }
79      
80      public static void log(String type, String format, Object... args)
81      {
82          err.printf(type + ": " + format + "%n",args);
83      }
84  
85      public static void info(String format, Object... args)
86      {
87          log("INFO",format,args);
88      }
89  
90      public static void warn(String format, Object... args)
91      {
92          log("WARNING",format,args);
93      }
94  
95      public static void warn(Throwable t)
96      {
97          t.printStackTrace(err);
98      }
99  
100     public static boolean isDebugEnabled()
101     {
102         return INSTANCE.debug;
103     }
104 
105     private boolean trace = false;
106     private boolean debug = false;
107 
108     public void initialize(BaseHome baseHome, CommandLineConfigSource cmdLineSource) throws IOException
109     {
110         String dbgProp = cmdLineSource.getProperty("debug");
111         if (dbgProp != null)
112         {
113             debug = Boolean.parseBoolean(dbgProp);
114         }
115 
116         String logFileName = cmdLineSource.getProperty("start-log-file");
117 
118         for (RawArgs.Entry arg : cmdLineSource.getArgs())
119         {
120             if ("--debug".equals(arg.getLine()))
121             {
122                 debug = true;
123                 continue;
124             }
125 
126             if (arg.startsWith("--start-log-file"))
127             {
128                 logFileName = Props.getValue(arg.getLine());
129                 continue;
130             }
131         }
132 
133         if (logFileName != null)
134         {
135             Path logfile = baseHome.getPath(logFileName);
136             logfile = logfile.toAbsolutePath();
137             initLogFile(logfile);
138         }
139     }
140 
141     public void initLogFile(Path logfile) throws IOException
142     {
143         if (logfile != null)
144         {
145             try
146             {
147                 Path logDir = logfile.getParent();
148                 FS.ensureDirectoryWritable(logDir);
149 
150                 Path startLog = logfile;
151 
152                 if (!FS.exists(startLog) && !FS.createNewFile(startLog))
153                 {
154                     // Output about error is lost in majority of cases.
155                     throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to create: " + startLog.toAbsolutePath()));
156                 }
157 
158                 if (!FS.canWrite(startLog))
159                 {
160                     // Output about error is lost in majority of cases.
161                     throw new UsageException(UsageException.ERR_LOGGING,new IOException("Unable to write to: " + startLog.toAbsolutePath()));
162                 }
163 
164                 err.println("StartLog to " + logfile);
165                 OutputStream fileout = Files.newOutputStream(startLog,StandardOpenOption.CREATE,StandardOpenOption.APPEND);
166                 PrintStream logger = new PrintStream(fileout);
167                 out=logger;
168                 err=logger;
169                 System.setErr(logger);
170                 System.setOut(logger);
171                 err.println("StartLog Establishing " + logfile + " on " + new Date());
172             }
173             catch (IOException e)
174             {
175                 throw new UsageException(UsageException.ERR_LOGGING,e);
176             }
177         }
178     }
179 
180     public static void enableDebug()
181     {
182         getInstance().debug = true;
183     }
184     
185     public static void endStartLog()
186     {
187         if (stderr!=err && getInstance().debug)
188         {
189             err.println("StartLog ended");
190             stderr.println("StartLog ended");
191         }
192         System.setErr(stderr);
193         System.setOut(stdout);
194     }
195 }