Force a Log File Rollover on WebSphere Application Server

Force a Log File Rollover on WebSphere Application Server

Do you ever get in the middle of development, or attempting to diagnose a problem on a server, and wish you could just clear out the logs without having to stop your server? I do all the time! As a result, I created a simple jython script that allows me to force a log file rollover on WebSphere Application Server effectively clearing out the current log file, and allowing me to start with a new, fresh log file.

The script I created, shown below, will get the applicable server information, specify a rollover log file location and file name, and force the server to rollover the SystemOut.log file.

I realize that this script could be optimized and improved upon, but for my basic needs of doing development and debugging it works just fine.

NOTE: The script currently forces the rolled over file to the base folder of your hard drive, and creates a unique name for that file consisting of the cell, node, server, and current timestamp in case you want to reference it later. I would like to modify this script to retrieve the appropriate environment variables from the server to resolve the proper SERVER_LOG_ROOT location, but have not taken those steps at this time. If you’d like to contribute that code, please comment below. :)

NOTE: The script below is targeted towards the SystemOut.log stream/file, but could easily be modified to include the SystemErr.log stream/file.

import sys,java
from java.util import Date as javaDate
from java.lang import System as javaSystem
from java.text import SimpleDateFormat as simpleDateFormat

# Get Line Separator
lineSep = java.lang.System.getProperty('line.separator')

# Get Current Cell
cell = AdminControl.getCell();
cellid = AdminConfig.getid('/Cell:'+ cell + '/');

# Get Current Node
node = AdminControl.getNode();
nodeid = AdminConfig.getid('/Cell:' + cell + '/Node:' + node + '/');

# Get All Servers For Current Node
allServers = AdminConfig.getid('/Node:' + node + '/Server:/')
allServersArray = allServers.split(lineSeparator);

# Iterate Over All Servers
for serverData in allServersArray:

	# Get Required Vars
	serverName = AdminConfig.showAttribute(serverData, 'name');
	rollOverDate = javaDate(javaSystem.currentTimeMillis());
	systemOutRolloverLogFile = '/SystemOut_' + cell + '_' + node + '_' + serverName + '_' + simpleDateFormat('yyyyMMddHHmmss').format(rollOverDate) + '.log';

	# Execute RollOver
	AdminControl.invoke(AdminControl.completeObjectName('type=TraceService,process=' + serverName + ',*'),
		'rolloverLogFileImmediate', '[SystemOut ' + systemOutRolloverLogFile + ']');
	print 'rolloverLogFileImmediate Filename: ' + systemOutRolloverLogFile;
	print 'rolloverLogFileImmediate completed for /Cell:' + cell + '/Node:' + node + '/Server:' + serverName;

To execute the log file rollover script, place the file somewhere on your system from which it can be read. Next, navigate to the WebSphere Application Server profile bin folder of the server you will be targeting. From that bin folder, execute the following command properly referencing the rollover log script in its readable location.

wsadmin -f rolloverLogFileImmediate.jy

The script can be downloaded as a plain text file here: rolloverLogFileImmediate.txt. You’ll want to modify the file extension to .py or .jy to get it to run using the wsadmin command.

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

Leave a Reply

Your email address will not be published.