001/* 002 * Copyright (c) 2016 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 cascading.flow.FlowProcess; 025import cascading.flow.FlowRuntimeProps; 026import org.slf4j.Logger; 027 028/** 029 * 030 */ 031public class LogUtil 032 { 033 public static String[] setLog4jLevel( String[] contexts, String level ) 034 { 035 String[] results = new String[ contexts.length ]; 036 037 for( int i = 0; i < contexts.length; i++ ) 038 results[ i ] = setLog4jLevel( contexts[ i ], level ); 039 040 return results; 041 } 042 043 public static String[] setLog4jLevel( String[] contexts, String[] levels ) 044 { 045 String[] results = new String[ contexts.length ]; 046 047 for( int i = 0; i < contexts.length; i++ ) 048 results[ i ] = setLog4jLevel( contexts[ i ], levels[ i ] ); 049 050 return results; 051 } 052 053 public static void setLog4jLevel( String[] logger ) 054 { 055 setLog4jLevel( logger[ 0 ], logger[ 1 ] ); 056 } 057 058 public static String setLog4jLevel( String logger, String level ) 059 { 060 // removing logj4 dependency 061 // org.apache.log4j.Logger.getLogger( logger[ 0 ] ).setLevel( org.apache.log4j.Level.toLevel( logger[ 1 ] ) ); 062 063 Object loggerObject = Util.invokeStaticMethod( "org.apache.log4j.Logger", "getLogger", 064 new Object[]{logger}, new Class[]{String.class} ); 065 066 Object levelObject = null; 067 068 if( level != null ) 069 levelObject = Util.invokeStaticMethod( "org.apache.log4j.Level", "toLevel", 070 new Object[]{level}, new Class[]{String.class} ); 071 072 Object oldLevel = Util.invokeInstanceMethod( loggerObject, "getLevel", 073 new Object[]{}, new Class[]{} ); 074 075 Util.invokeInstanceMethod( loggerObject, "setLevel", 076 new Object[]{levelObject}, new Class[]{Util.loadClass( "org.apache.log4j.Level" )} ); 077 078 if( oldLevel == null ) 079 return null; 080 081 return oldLevel.toString(); 082 } 083 084 public static void logMemory( Logger logger, String message ) 085 { 086 Runtime runtime = Runtime.getRuntime(); 087 long freeMem = runtime.freeMemory() / 1024 / 1024; 088 long maxMem = runtime.maxMemory() / 1024 / 1024; 089 long totalMem = runtime.totalMemory() / 1024 / 1024; 090 091 logger.info( message + " (mb), free: " + freeMem + ", total: " + totalMem + ", max: " + maxMem ); 092 } 093 094 public static void logCounters( Logger logger, String message, FlowProcess flowProcess ) 095 { 096 String counters = flowProcess.getStringProperty( FlowRuntimeProps.LOG_COUNTERS ); 097 098 if( counters == null ) 099 return; 100 101 String[] split = counters.split( "," ); 102 103 for( String value : split ) 104 { 105 String counter[] = value.split( ":" ); 106 107 logger.info( "{} {}.{}={}", message, counter[ 0 ], counter[ 1 ], flowProcess.getCounterValue( counter[ 0 ], counter[ 1 ] ) ); 108 } 109 } 110 }