1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.giraph.utils;
19
20 import java.io.IOException;
21 import org.apache.hadoop.mapreduce.TaskAttemptContext;
22 import org.apache.log4j.Level;
23 import org.apache.log4j.Logger;
24
25 /**
26 * Logger utils for log4j
27 */
28 public class LoggerUtils {
29 /**
30 * Don't construct this.
31 */
32 private LoggerUtils() { }
33
34 /**
35 * Helper method to set the status and log message together if condition
36 * has been been met.
37 *
38 * @param condition Must be true to write status and log
39 * @param context Context to set the status with
40 * @param logger Logger to write to
41 * @param level Level of logging
42 * @param message Message to set status with
43 */
44 public static void conditionalSetStatusAndLog(
45 boolean condition,
46 TaskAttemptContext context, Logger logger, Level level,
47 String message) {
48 if (condition) {
49 setStatusAndLog(context, logger, level, message);
50 }
51 }
52
53 /**
54 * Helper method to set the status and log message together.
55 *
56 * @param context Context to set the status with
57 * @param logger Logger to write to
58 * @param level Level of logging
59 * @param message Message to set status with
60 */
61 public static void setStatusAndLog(
62 TaskAttemptContext context, Logger logger, Level level,
63 String message) {
64 try {
65 setStatus(context, message);
66 } catch (IOException e) {
67 throw new IllegalStateException("setStatusAndLog: Got IOException", e);
68 }
69 if (logger.isEnabledFor(level)) {
70 logger.log(level, message);
71 }
72 }
73
74 /**
75 * Set Hadoop status message.
76 *
77 * NOTE: In theory this function could get folded in to the callsites, but
78 * the issue is that some Hadoop jars, e.g. 0.23 and 2.0.0, don't actually
79 * throw IOException on setStatus while others do. This makes wrapping it in a
80 * try/catch cause a compile error on those Hadoops. With this function every
81 * caller sees a method that throws IOException. In case it doesn't actually,
82 * there is no more compiler error because not throwing a decalred exception
83 * is at best a warning.
84 *
85 * @param context Context to set the status with
86 * @param message Message to set status with
87 * @throws IOException If something goes wrong with setting status message
88 */
89 private static void setStatus(TaskAttemptContext context, String message)
90 throws IOException {
91 context.setStatus(message);
92 }
93 }