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