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 */
018package org.apache.oozie.util;
019
020import org.apache.oozie.AppType;
021import org.apache.oozie.BundleJobBean;
022import org.apache.oozie.CoordinatorActionBean;
023import org.apache.oozie.CoordinatorJobBean;
024import org.apache.oozie.WorkflowActionBean;
025import org.apache.oozie.WorkflowJobBean;
026import org.apache.oozie.client.WorkflowAction;
027import org.apache.oozie.client.event.Event;
028import org.apache.oozie.client.event.JobEvent;
029import org.apache.oozie.client.event.SLAEvent;
030import org.apache.oozie.service.DagXLogInfoService;
031import org.apache.oozie.service.Services;
032import org.apache.oozie.service.UUIDService;
033import org.apache.oozie.service.XLogService;
034
035/**
036 * logging utilities.
037 */
038public class LogUtils {
039
040    /**
041     * Set the log info with the context of the given coordinator bean.
042     *
043     * @param cBean coordinator bean.
044     * @param logInfo log info
045     */
046    public static void setLogInfo(CoordinatorJobBean cBean, XLog.Info logInfo) {
047        if (logInfo.getParameter(XLogService.GROUP) == null) {
048            logInfo.setParameter(XLogService.GROUP, cBean.getGroup());
049        }
050        if (logInfo.getParameter(XLogService.USER) == null) {
051            logInfo.setParameter(XLogService.USER, cBean.getUser());
052        }
053        logInfo.setParameter(DagXLogInfoService.JOB, cBean.getId());
054        logInfo.setParameter(DagXLogInfoService.TOKEN, "");
055        logInfo.setParameter(DagXLogInfoService.APP, cBean.getAppName());
056        XLog.Info.get().setParameters(logInfo);
057    }
058
059    /**
060     * Set the log info with the context of the given coordinator action bean.
061     *
062     * @param action action bean.
063     * @param logInfo log info
064     */
065    public static void setLogInfo(CoordinatorActionBean action, XLog.Info logInfo) {
066        logInfo.setParameter(DagXLogInfoService.JOB, action.getJobId());
067        logInfo.setParameter(DagXLogInfoService.ACTION, action.getId());
068        XLog.Info.get().setParameters(logInfo);
069    }
070
071    /**
072     * Set the log info with the context of the given workflow bean.
073     *
074     * @param workflow workflow bean.
075     * @param logInfo log info
076     */
077    public static void setLogInfo(WorkflowJobBean workflow, XLog.Info logInfo) {
078        logInfo.setParameter(XLogService.GROUP, workflow.getGroup());
079        logInfo.setParameter(XLogService.USER, workflow.getUser());
080        logInfo.setParameter(DagXLogInfoService.JOB, workflow.getId());
081        logInfo.setParameter(DagXLogInfoService.TOKEN, workflow.getLogToken());
082        logInfo.setParameter(DagXLogInfoService.APP, workflow.getAppName());
083        XLog.Info.get().setParameters(logInfo);
084    }
085
086    /**
087     * Set the log info with the context of the given action bean.
088     *
089     * @param action action bean.
090     * @param logInfo log info
091     */
092    public static void setLogInfo(WorkflowActionBean action, XLog.Info logInfo) {
093        logInfo.setParameter(DagXLogInfoService.JOB, action.getJobId());
094        logInfo.setParameter(DagXLogInfoService.TOKEN, action.getLogToken());
095        logInfo.setParameter(DagXLogInfoService.ACTION, action.getId());
096        XLog.Info.get().setParameters(logInfo);
097    }
098
099    public static void setLogInfo(WorkflowAction action, XLog.Info logInfo) {
100        String actionId = action.getId();
101        logInfo.setParameter(DagXLogInfoService.JOB, actionId.substring(0, actionId.indexOf("@")));
102        logInfo.setParameter(DagXLogInfoService.ACTION, actionId);
103        XLog.Info.get().setParameters(logInfo);
104    }
105
106    /**
107     * Set the log info with the context of the given bundle bean.
108     *
109     * @param bBean bundle bean.
110     * @param logInfo log info
111     */
112    public static void setLogInfo(BundleJobBean bBean, XLog.Info logInfo) {
113        if (logInfo.getParameter(XLogService.GROUP) == null) {
114            logInfo.setParameter(XLogService.GROUP, bBean.getGroup());
115        }
116        if (logInfo.getParameter(XLogService.USER) == null) {
117            logInfo.setParameter(XLogService.USER, bBean.getUser());
118        }
119        logInfo.setParameter(DagXLogInfoService.JOB, bBean.getId());
120        logInfo.setParameter(DagXLogInfoService.TOKEN, "");
121        logInfo.setParameter(DagXLogInfoService.APP, bBean.getAppName());
122        XLog.Info.get().setParameters(logInfo);
123    }
124
125    /**
126     * Set the thread local log info with the context of the given Info object.
127     *
128     * @param logInfo log info
129     */
130    public static void setLogInfo(XLog.Info logInfo) {
131        XLog.Info.get().setParameters(logInfo);
132    }
133
134    public static XLog setLogInfo(XLog logObj, String jobId, String actionId, String appName) {
135        clearLogPrefix();
136        XLog.Info logInfo = XLog.Info.get();
137        logInfo.setParameter(DagXLogInfoService.JOB, jobId);
138        if (actionId != null) {
139            logInfo.setParameter(DagXLogInfoService.ACTION, actionId);
140        }
141        if (appName != null) {
142            logInfo.setParameter(DagXLogInfoService.APP, appName);
143        }
144        return XLog.resetPrefix(logObj);
145    }
146
147    public static XLog setLogPrefix(XLog logObj, Event event) {
148        String jobId = null, actionId = null, appName = null;
149        if (event instanceof JobEvent) {
150            JobEvent je = (JobEvent) event;
151            if (je.getAppType() == AppType.WORKFLOW_JOB || je.getAppType() == AppType.COORDINATOR_JOB
152                    || je.getAppType() == AppType.BUNDLE_JOB) {
153                jobId = je.getId();
154            }
155            else {
156                actionId = je.getId();
157                jobId = Services.get().get(UUIDService.class).getId(actionId);
158            }
159            appName = je.getAppName();
160        }
161        else if (event instanceof SLAEvent) {
162            SLAEvent se = (SLAEvent) event;
163            if (se.getAppType() == AppType.WORKFLOW_JOB || se.getAppType() == AppType.COORDINATOR_JOB
164                    || se.getAppType() == AppType.BUNDLE_JOB) {
165                jobId = se.getId();
166            }
167            else {
168                actionId = se.getId();
169                jobId = Services.get().get(UUIDService.class).getId(actionId);
170            }
171            appName = se.getAppName();
172        }
173        return LogUtils.setLogInfo(logObj, jobId, actionId, appName);
174    }
175
176    public static void clearLogPrefix() {
177        XLog.Info.get().clearParameter(DagXLogInfoService.JOB);
178        XLog.Info.get().clearParameter(DagXLogInfoService.ACTION);
179        XLog.Info.get().clearParameter(DagXLogInfoService.APP);
180    }
181
182}