View Javadoc

1   package org.apache.helix.tools;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.lang.management.ManagementFactory;
25  import java.lang.management.RuntimeMXBean;
26  import java.text.SimpleDateFormat;
27  import java.util.Calendar;
28  import java.util.Date;
29  import java.util.List;
30  
31  import org.apache.log4j.FileAppender;
32  import org.apache.log4j.Layout;
33  import org.apache.log4j.spi.ErrorCode;
34  
35  public class CLMLogFileAppender extends FileAppender
36  {
37    public CLMLogFileAppender()
38    {
39    }
40  
41    public CLMLogFileAppender(Layout layout, String filename, boolean append,
42        boolean bufferedIO, int bufferSize) throws IOException
43    {
44      super(layout, filename, append, bufferedIO, bufferSize);
45    }
46  
47    public CLMLogFileAppender(Layout layout, String filename, boolean append)
48        throws IOException
49    {
50      super(layout, filename, append);
51    }
52  
53    public CLMLogFileAppender(Layout layout, String filename) throws IOException
54    {
55      super(layout, filename);
56    }
57  
58    public void activateOptions()
59    {
60      if (fileName != null)
61      {
62        try
63        {
64          fileName = getNewLogFileName();
65          setFile(fileName, fileAppend, bufferedIO, bufferSize);
66        } catch (Exception e)
67        {
68          errorHandler.error("Error while activating log options", e,
69              ErrorCode.FILE_OPEN_FAILURE);
70        }
71      }
72    }
73  
74    private String getNewLogFileName()
75    {
76      Calendar cal = Calendar.getInstance();
77      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");
78      String time = sdf.format(cal.getTime());
79  
80      StackTraceElement[] stack = Thread.currentThread().getStackTrace();
81      StackTraceElement main = stack[stack.length - 1];
82      String mainClass = main.getClassName();
83  
84      return System.getProperty("user.home") + "/EspressoLogs/" + mainClass + "_"
85          + time + ".txt";
86    }
87  }