Manually rotate WebLogic Server Logfiles

Posted by Dirk Nachbar on Tuesday, October 24, 2017
From time to time you have the requirement to rotate the WebLogic Server Logfiles manually on the fly without stopping the WebLogic Managed Servers or AdminServer, for example while you perform some load tests in order to have fresh WebLogic Server Logfiles.

Here is a small Python Script, which rotates the WebLogic Server Logfiles for a list of given Managed Servers and AdminServer.

You will need:
  • rotatelogs.py = Python Script which rotates the Logfiles
  • domain.properties = Property File for the required connect data to the Admin Server of your WebLogic Domain
The rotatelogs.py Python Script will read the Property File (parameter -props) in order to get the necessary data for the connect to the Admin Server of your WebLogic Server Domain and will loop through your provided list of Servers (parameter -servers) and force a rotating of the logfiles. The list of Servers (parameter -servers) can accept multiple Servers delimited by ":".

domain.properties

# Align the values for your settings
admin.url=localhost:7001
admin.userName=weblogic
admin.password=welcome01

rotatelogs.py

# ============================================================
#
# Script: rotatelogs.py
#
# Author: Dirk Nachbar, https/dirknachbar.blogspot.ch
#
# Purpose: Rotates the WebLogic Server Logfiles for a list
#          of given Servers, either AdminServer or Managed Servers
#
# ============================================================

# Imports 
import sys
from java.io import File
from java.io import FileOutputStream
from java.io import FileInputStream

servers_list=''
configfile=''

def helpUsage():
    print 'Usage: rotatelogs.py -help'
    print '          [-servers] list of Servers, delimited by :'
    print '          [-props] properties file for connect to AdminServer'
    print 'E.g.:  wlst.sh rotatelogs.py -servers DEMOMS1:DEMOMS2 -props /home/oracle/domain.properties'
    exit()

def connectAdmin():
    connect(adminUserName,adminPassword,adminURL)

for i in range(len(sys.argv)):
    if sys.argv[i] == "-help":
        helpUsage()
    elif sys.argv[i] == "-servers":
        if i+1 < len(sys.argv):
            servers_list = sys.argv[i+1]
    elif sys.argv[i] == "-props":
        if i+1 < len(sys.argv):
            configfile = sys.argv[i+1]

if len(servers_list)==0 or len(configfile)==0:
    print 'Missing required arguments (-servers, -props)'
    print ''
    helpUsage()

propInputStream = FileInputStream(configfile)
configProps = Properties()
configProps.load(propInputStream)
adminURL=configProps.get("admin.url")
adminUserName=configProps.get("admin.userName")
adminPassword=configProps.get("admin.password")

connectAdmin()

domainRuntime()

servers=servers_list.split(":")

for server in servers:
    print '================================================'
    print 'Current Server: ' +server
    cd('/ServerRuntimes/'+server+'/ServerLogRuntime/'+server)
    print 'Rotating Logfile for Server: ' +server
    cmo.forceLogRotation()
    print '================================================'

Create the 2 above given files on your Server hosting the Oracle WebLogic Domain, align the domain.properties file with your settings, connect to your Server with the Oracle Software User and execute the script as following:

oracle@server> $ORACLE_HOME/oracle_common/common/bin/wlst.sh rotatelogs.py -props domain.properties -servers DEMOMS1:DEMOMS2
Initializing WebLogic Scripting Tool (WLST) ...

Welcome to WebLogic Server Administration Scripting Shell

Type help() for help on available commands

Connecting to t3://localhost:7001 with userid weblogic ...
Successfully connected to Admin Server "DemoAdminServer" that belongs to domain "demo_domain".

Warning: An insecure protocol was used to connect to the server. 
To ensure on-the-wire security, the SSL port or Admin port should be used instead.

Location changed to domainRuntime tree. This is a read-only tree 
with DomainMBean as the root MBean. 
For more help, use help('domainRuntime')

================================================
Current Server: DEMOMS1
Rotating Logfile for Server: DEMOMS1
================================================
================================================
Current Server: DEMOMS2
Rotating Logfile for Server: DEMOMS2
================================================

Happy rotating of WebLogic Server Logfiles . . .