001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.service;
020
021import java.util.regex.Pattern;
022
023import org.apache.commons.logging.LogFactory;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.oozie.util.XLog;
026
027public class XLogUtil {
028
029    private String logPath;
030    private String logFileName;
031    private boolean isLogOverEnable = true;
032    private int logRotation;
033
034    public static XLog log = new XLog(LogFactory.getLog(XLogUtil.class));
035
036    public XLogUtil(Configuration conf, String logType) {
037        extractInfoForLogWebService(conf, logType);
038    }
039
040    private void extractInfoForLogWebService(Configuration conf, String logType) {
041        String logFile = conf.get("log4j.appender." + logType + ".File");
042        if (logFile == null) {
043            log.warn("Oozie WS " + logType
044                    + " log will be disabled, missing property 'log4j.appender.oozie.File' for 'oozie' " + "appender");
045            isLogOverEnable = false;
046        }
047        else {
048            logFile = logFile.trim();
049            int i = logFile.lastIndexOf("/");
050            if (i == -1) {
051                log.warn("Oozie WS " + logType
052                        + " log will be disabled, log file is not an absolute path [{0}] for 'oozie' appender", logFile);
053                isLogOverEnable = false;
054            }
055            else {
056                String appenderClass = conf.get("log4j.appender." + logType);
057                if (appenderClass == null) {
058                    log.warn("Oozie WS " + logType + " log will be disabled, missing property [log4j.appender.oozie]");
059                    isLogOverEnable = false;
060                }
061                else if (appenderClass.equals("org.apache.log4j.DailyRollingFileAppender")) {
062                    String pattern = conf.get("log4j.appender." + logType + ".DatePattern");
063                    if (pattern == null) {
064                        log.warn("Oozie WS " + logType
065                                + " log will be disabled, missing property [log4j.appender." + logType + ".DatePattern]");
066                        isLogOverEnable = false;
067                    }
068                    else {
069                        pattern = pattern.trim();
070                        if (pattern.endsWith("HH")) {
071                            logRotation = 60 * 60;
072                        }
073                        else if (pattern.endsWith("dd")) {
074                            logRotation = 60 * 60 * 24;
075                        }
076                        else {
077                            log.warn("Oozie WS " + logType
078                                    + " log will be disabled, DatePattern [{0}] should end with 'HH' or 'dd'", pattern);
079                            isLogOverEnable = false;
080                        }
081                        if (logRotation > 0) {
082                            logPath = logFile.substring(0, i);
083                            logFileName = logFile.substring(i + 1);
084                        }
085                    }
086                }
087                else if (appenderClass.equals("org.apache.log4j.rolling.RollingFileAppender")) {
088                    String pattern = conf.get("log4j.appender." + logType + ".RollingPolicy.FileNamePattern");
089                    if (pattern == null) {
090                        log.warn("Oozie WS " + logType + " log will be disabled, missing property "
091                                + "[log4j.appender." + logType + ".RollingPolicy.FileNamePattern]");
092                        isLogOverEnable = false;
093                    }
094                    else {
095                        pattern = pattern.trim();
096                        if (pattern.matches(Pattern.quote(logFile) + ".*-%d\\{yyyy-MM-dd-HH\\}(\\.gz)?")) {
097                            logRotation = 60 * 60;
098                        }
099                        else if (pattern.matches(Pattern.quote(logFile) + ".*-%d\\{yyyy-MM-dd\\}(\\.gz)?")
100                                || pattern.matches(Pattern.quote(logFile) + ".*\\.%d\\{yyyy-MM-dd\\}(\\.gz)?")) {
101                            logRotation = 60 * 60 * 24;
102                        }
103
104                        else {
105                            log.warn(
106                                    "Oozie WS "
107                                            + logType
108                                            + " log will be disabled, RollingPolicy.FileNamePattern [{0}] should end with "
109                                            + "'-%d{yyyy-MM-dd-HH}' or '-%d{yyyy-MM-dd-HH}.gz' and also start with the value of "
110                                            + "log4j.appender." + logType + ".File [{1}]", pattern, logFile);
111                            isLogOverEnable = false;
112                        }
113                        if (logRotation > 0) {
114                            logPath = logFile.substring(0, i);
115                            logFileName = logFile.substring(i + 1);
116                        }
117                    }
118                }
119                else {
120                    log.warn("Oozie WS "
121                            + logType
122                            + " log will be disabled, log4j.appender.oozie ["
123                            + appenderClass
124                            + "] should be "
125                            + "either org.apache.log4j.DailyRollingFileAppender or org.apache.log4j.rolling.RollingFileAppender "
126                            + "to enable it");
127                    isLogOverEnable = false;
128                }
129            }
130        }
131    }
132
133    public String getLogPath() {
134        return logPath;
135    }
136
137    public String getLogFileName() {
138        return logFileName;
139    }
140
141    public boolean isLogOverEnable() {
142        return isLogOverEnable;
143    }
144
145    public int getLogRotation() {
146        return logRotation;
147    }
148
149}