001/* 002 * Copyright (c) 2007-2016 Concurrent, Inc. All Rights Reserved. 003 * 004 * Project and contact information: http://www.cascading.org/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021package cascading.util; 022 023import cascading.flow.FlowProcess; 024import cascading.flow.FlowRuntimeProps; 025import org.slf4j.Logger; 026 027/** 028 * 029 */ 030public class LogUtil 031 { 032 public static void setLog4jLevel( String[] logger ) 033 { 034 setLog4jLevel( logger[ 0 ], logger[ 1 ] ); 035 } 036 037 public static void setLog4jLevel( String logger, String level ) 038 { 039 // removing logj4 dependency 040 // org.apache.log4j.Logger.getLogger( logger[ 0 ] ).setLevel( org.apache.log4j.Level.toLevel( logger[ 1 ] ) ); 041 042 Object loggerObject = Util.invokeStaticMethod( "org.apache.log4j.Logger", "getLogger", 043 new Object[]{logger}, new Class[]{String.class} ); 044 045 Object levelObject = Util.invokeStaticMethod( "org.apache.log4j.Level", "toLevel", 046 new Object[]{level}, new Class[]{String.class} ); 047 048 Util.invokeInstanceMethod( loggerObject, "setLevel", 049 new Object[]{levelObject}, new Class[]{levelObject.getClass()} ); 050 } 051 052 public static void logMemory( Logger logger, String message ) 053 { 054 Runtime runtime = Runtime.getRuntime(); 055 long freeMem = runtime.freeMemory() / 1024 / 1024; 056 long maxMem = runtime.maxMemory() / 1024 / 1024; 057 long totalMem = runtime.totalMemory() / 1024 / 1024; 058 059 logger.info( message + " (mb), free: " + freeMem + ", total: " + totalMem + ", max: " + maxMem ); 060 } 061 062 public static void logCounters( Logger logger, String message, FlowProcess flowProcess ) 063 { 064 String counters = flowProcess.getStringProperty( FlowRuntimeProps.LOG_COUNTERS ); 065 066 if( counters == null ) 067 return; 068 069 String[] split = counters.split( "," ); 070 071 for( String value : split ) 072 { 073 String counter[] = value.split( ":" ); 074 075 logger.info( "{} {}.{}={}", message, counter[ 0 ], counter[ 1 ], flowProcess.getCounterValue( counter[ 0 ], counter[ 1 ] ) ); 076 } 077 } 078 }