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 { 100 log.warn( 101 "Oozie WS " 102 + logType 103 + " log will be disabled, RollingPolicy.FileNamePattern [{0}] should end with " 104 + "'-%d{yyyy-MM-dd-HH}' or '-%d{yyyy-MM-dd-HH}.gz' and also start with the value of " 105 + "log4j.appender." + logType + ".File [{1}]", pattern, logFile); 106 isLogOverEnable = false; 107 } 108 if (logRotation > 0) { 109 logPath = logFile.substring(0, i); 110 logFileName = logFile.substring(i + 1); 111 } 112 } 113 } 114 else { 115 log.warn("Oozie WS " 116 + logType 117 + " log will be disabled, log4j.appender.oozie [" 118 + appenderClass 119 + "] should be " 120 + "either org.apache.log4j.DailyRollingFileAppender or org.apache.log4j.rolling.RollingFileAppender " 121 + "to enable it"); 122 isLogOverEnable = false; 123 } 124 } 125 } 126 } 127 128 public String getLogPath() { 129 return logPath; 130 } 131 132 public String getLogFileName() { 133 return logFileName; 134 } 135 136 public boolean isLogOverEnable() { 137 return isLogOverEnable; 138 } 139 140 public int getLogRotation() { 141 return logRotation; 142 } 143 144}