001/* 002 * Copyright (c) 2016-2018 Chris K Wensel <chris@wensel.net>. All Rights Reserved. 003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved. 004 * 005 * Project and contact information: http://www.cascading.org/ 006 * 007 * This file is part of the Cascading project. 008 * 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 */ 021 022package cascading.util; 023 024import java.util.HashSet; 025import java.util.Set; 026 027import cascading.flow.FlowProcess; 028import cascading.flow.FlowRuntimeProps; 029import org.slf4j.Logger; 030 031/** 032 * 033 */ 034public class LogUtil 035 { 036 public static String[] setLog4jLevel( String[] contexts, String level ) 037 { 038 String[] results = new String[ contexts.length ]; 039 040 for( int i = 0; i < contexts.length; i++ ) 041 results[ i ] = setLog4jLevel( contexts[ i ], level ); 042 043 return results; 044 } 045 046 public static String[] setLog4jLevel( String[] contexts, String[] levels ) 047 { 048 String[] results = new String[ contexts.length ]; 049 050 for( int i = 0; i < contexts.length; i++ ) 051 results[ i ] = setLog4jLevel( contexts[ i ], levels[ i ] ); 052 053 return results; 054 } 055 056 public static void setLog4jLevel( String[] logger ) 057 { 058 setLog4jLevel( logger[ 0 ], logger[ 1 ] ); 059 } 060 061 public static String setLog4jLevel( String logger, String level ) 062 { 063 // removing logj4 dependency 064 // org.apache.log4j.Logger.getLogger( logger[ 0 ] ).setLevel( org.apache.log4j.Level.toLevel( logger[ 1 ] ) ); 065 066 Object loggerObject = Util.invokeStaticMethod( "org.apache.log4j.Logger", "getLogger", 067 new Object[]{logger}, new Class[]{String.class} ); 068 069 Object levelObject = null; 070 071 if( level != null ) 072 levelObject = Util.invokeStaticMethod( "org.apache.log4j.Level", "toLevel", 073 new Object[]{level}, new Class[]{String.class} ); 074 075 Object oldLevel = Util.invokeInstanceMethod( loggerObject, "getLevel", 076 new Object[]{}, new Class[]{} ); 077 078 Util.invokeInstanceMethod( loggerObject, "setLevel", 079 new Object[]{levelObject}, new Class[]{Util.loadClass( "org.apache.log4j.Level" )} ); 080 081 if( oldLevel == null ) 082 return null; 083 084 return oldLevel.toString(); 085 } 086 087 public static void logMemory( Logger logger, String message ) 088 { 089 Runtime runtime = Runtime.getRuntime(); 090 long freeMem = runtime.freeMemory() / 1024 / 1024; 091 long maxMem = runtime.maxMemory() / 1024 / 1024; 092 long totalMem = runtime.totalMemory() / 1024 / 1024; 093 094 logger.info( message + " (mb), free: " + freeMem + ", total: " + totalMem + ", max: " + maxMem ); 095 } 096 097 public static void logCounters( Logger logger, String message, FlowProcess flowProcess ) 098 { 099 String counters = flowProcess.getStringProperty( FlowRuntimeProps.LOG_COUNTERS ); 100 101 if( counters == null ) 102 return; 103 104 String[] split = counters.split( "," ); 105 106 for( String value : split ) 107 { 108 String counter[] = value.split( ":" ); 109 110 logger.info( "{} {}.{}={}", message, counter[ 0 ], counter[ 1 ], flowProcess.getCounterValue( counter[ 0 ], counter[ 1 ] ) ); 111 } 112 } 113 114 static Set<String> cache = new HashSet<>(); 115 116 public static void logWarnOnce( Logger log, String message, Object arg ) 117 { 118 if( cache.add( log.getName() + message ) ) 119 log.warn( message, arg ); 120 } 121 }