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    package org.apache.oozie.util;
019    
020    import org.apache.oozie.BundleJobBean;
021    import org.apache.oozie.CoordinatorActionBean;
022    import org.apache.oozie.CoordinatorJobBean;
023    import org.apache.oozie.WorkflowActionBean;
024    import org.apache.oozie.WorkflowJobBean;
025    import org.apache.oozie.service.DagXLogInfoService;
026    import org.apache.oozie.service.XLogService;
027    
028    /**
029     * logging utilities.
030     */
031    public class LogUtils {
032    
033        /**
034         * Set the log info with the context of the given coordinator bean.
035         *
036         * @param cBean coordinator bean.
037         * @param logInfo log info
038         */
039        public static void setLogInfo(CoordinatorJobBean cBean, XLog.Info logInfo) {
040            if (logInfo.getParameter(XLogService.GROUP) == null) {
041                logInfo.setParameter(XLogService.GROUP, cBean.getGroup());
042            }
043            if (logInfo.getParameter(XLogService.USER) == null) {
044                logInfo.setParameter(XLogService.USER, cBean.getUser());
045            }
046            logInfo.setParameter(DagXLogInfoService.JOB, cBean.getId());
047            logInfo.setParameter(DagXLogInfoService.TOKEN, "");
048            logInfo.setParameter(DagXLogInfoService.APP, cBean.getAppName());
049            XLog.Info.get().setParameters(logInfo);
050        }
051    
052        /**
053         * Set the log info with the context of the given coordinator action bean.
054         *
055         * @param action action bean.
056         * @param logInfo log info
057         */
058        public static void setLogInfo(CoordinatorActionBean action, XLog.Info logInfo) {
059            logInfo.setParameter(DagXLogInfoService.JOB, action.getJobId());
060            logInfo.setParameter(DagXLogInfoService.ACTION, action.getId());
061            XLog.Info.get().setParameters(logInfo);
062        }
063    
064        /**
065         * Set the log info with the context of the given workflow bean.
066         *
067         * @param workflow workflow bean.
068         * @param logInfo log info
069         */
070        public static void setLogInfo(WorkflowJobBean workflow, XLog.Info logInfo) {
071            logInfo.setParameter(XLogService.GROUP, workflow.getGroup());
072            logInfo.setParameter(XLogService.USER, workflow.getUser());
073            logInfo.setParameter(DagXLogInfoService.JOB, workflow.getId());
074            logInfo.setParameter(DagXLogInfoService.TOKEN, workflow.getLogToken());
075            logInfo.setParameter(DagXLogInfoService.APP, workflow.getAppName());
076            XLog.Info.get().setParameters(logInfo);
077        }
078    
079        /**
080         * Set the log info with the context of the given action bean.
081         *
082         * @param action action bean.
083         * @param logInfo log info
084         */
085        public static void setLogInfo(WorkflowActionBean action, XLog.Info logInfo) {
086            logInfo.setParameter(DagXLogInfoService.JOB, action.getJobId());
087            logInfo.setParameter(DagXLogInfoService.TOKEN, action.getLogToken());
088            logInfo.setParameter(DagXLogInfoService.ACTION, action.getId());
089            XLog.Info.get().setParameters(logInfo);
090        }
091    
092        /**
093         * Set the log info with the context of the given bundle bean.
094         *
095         * @param bBean bundle bean.
096         * @param logInfo log info
097         */
098        public static void setLogInfo(BundleJobBean bBean, XLog.Info logInfo) {
099            if (logInfo.getParameter(XLogService.GROUP) == null) {
100                logInfo.setParameter(XLogService.GROUP, bBean.getGroup());
101            }
102            if (logInfo.getParameter(XLogService.USER) == null) {
103                logInfo.setParameter(XLogService.USER, bBean.getUser());
104            }
105            logInfo.setParameter(DagXLogInfoService.JOB, bBean.getId());
106            logInfo.setParameter(DagXLogInfoService.TOKEN, "");
107            logInfo.setParameter(DagXLogInfoService.APP, bBean.getAppName());
108            XLog.Info.get().setParameters(logInfo);
109        }
110    
111        /**
112         * Set the thread local log info with the context of the given Info object.
113         *
114         * @param logInfo log info
115         */
116        public static void setLogInfo(XLog.Info logInfo) {
117            XLog.Info.get().setParameters(logInfo);
118        }
119    
120    }