WSO2 ESB 4.9.0에서 정상적으로 작동했습니다.
그러나 우리가 필요로하는 다른 기능 중 일부가 손상되어 업그레이드 할 수있는 옵션이 없습니다. 마지막으로 wso2 작업에서 로그 회전 동작을 시뮬레이션했습니다. 샘플 WSo2 작업을 작성하는 방법을 이해하려면 https://docs.wso2.com/display/ESB481/Writing+Tasks+Sample을 참조하십시오.
이
은
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.ManagedLifecycle;
import org.apache.synapse.core.SynapseEnvironment;
import org.apache.synapse.task.Task;
public class LogFileBackupTaskSample implements Task, ManagedLifecycle
{
private static final Log LOG = LogFactory.getLog(LogFileBackupTask.class);
private SynapseEnvironment synapseEnvironment;
// By default, the program is assumed to run from the WSO2 Home Folder
public static final String DEFAULT_LOG_FILE_LOCATION ="repository/logs/";
public static final String WSO2_LOG_FILE_NAME ="wso2carbon.log";
// If a value is not provided for logFileSizeInMbStr, backup of the LOG file size would happen after it reaches 100 MB
public static final int DEFAULT_LOG_FILE_SIZE_IN_MB =100;
// If a value is not provided for noOfBackupFiles, 20 files would be backed up.
public static final int DEFAULT_BACKUP_FILES_NUMBER =20;
public static final int MB_TO_BYTES=1024 *1000;
private String logLocation;
private int logFileSizeInMb;
private String logFileSizeInMbStr;
private String noOfBackupFiles;
private int noOfBackupFilesInt;
public void execute()
{
FileChannel sourceChannel = null;
FileChannel outChannel = null;
if(logLocation==null || logLocation.trim().equals(""))
{
if(LOG.isInfoEnabled())
{
LOG.info("No LOG location provided. Therefore using the default location of "+DEFAULT_LOG_FILE_LOCATION);
}
logLocation= DEFAULT_LOG_FILE_LOCATION;
}
if(!logLocation.endsWith("/") && !logLocation.endsWith("\\"))
{
logLocation= logLocation+File.separator;
}
if(logFileSizeInMb==0)
{
if(LOG.isInfoEnabled())
{
LOG.info("No LOG file size in MB. Therefore using the default size of "+DEFAULT_LOG_FILE_SIZE_IN_MB+" Mb");
}
logFileSizeInMb= DEFAULT_LOG_FILE_SIZE_IN_MB * MB_TO_BYTES;
}
String inputLogFileNameWithDirectory= logLocation+WSO2_LOG_FILE_NAME;
File inputLogFileWithDirectory = new File(inputLogFileNameWithDirectory);
long currentLogSize=0;
boolean fileSwapped= false;
try
{
currentLogSize = inputLogFileWithDirectory.length();
if(currentLogSize> logFileSizeInMb)
{
long currentDateLong = System.currentTimeMillis();
Date date = new Date(currentDateLong);
String outFileName= WSO2_LOG_FILE_NAME+"."+date.toString().replace(' ', '_').replace(':', '_');
sourceChannel = new FileInputStream(inputLogFileWithDirectory).getChannel();
File outFile = new File (logLocation+outFileName);
outFile.createNewFile();
outChannel = new FileOutputStream(outFile).getChannel();
outChannel.transferFrom(sourceChannel, 0, currentLogSize);
fileSwapped= true;
}
}
catch(IOException e)
{
LOG.error(e.toString(),e);
throw new RuntimeException(e);
}
finally
{
if(sourceChannel!=null)
{
try
{
sourceChannel.close();
}
catch (IOException e) {
// Ignored
LOG.error(e.toString(),e);
}
}
if(outChannel!=null)
{
try
{
outChannel.close();
}
catch (IOException e) {
// Ignored
LOG.error(e.toString(),e);
}
}
}
FileChannel sourceTruncateChannel = null;
try
{
if(fileSwapped)
{
sourceTruncateChannel = new FileOutputStream(inputLogFileWithDirectory).getChannel();
sourceTruncateChannel.truncate(currentLogSize);
}
}
catch(IOException e)
{
LOG.error(e.toString(),e);
throw new RuntimeException(e);
}
finally
{
if(sourceTruncateChannel!=null)
{
try
{
sourceTruncateChannel.close();
}
catch (IOException e) {
// Ignored
LOG.error(e.toString(),e);
}
}
}
if(fileSwapped)
{
deletingOldFiles();
}
}
public void deletingOldFiles()
{
if(noOfBackupFilesInt==0)
{
if(LOG.isInfoEnabled())
{
LOG.info("NoOfBackupFiles 0. Thus using the default number of "+DEFAULT_BACKUP_FILES_NUMBER);
}
noOfBackupFilesInt= DEFAULT_BACKUP_FILES_NUMBER;
}
File[] listOfFiles= new File(logLocation).listFiles();
List <TimeStampWiseFile> listOfWso2Files = new ArrayList<TimeStampWiseFile>();
for (int i = 0; i < listOfFiles.length; i++)
{
if(listOfFiles[i].getName().startsWith(WSO2_LOG_FILE_NAME) && !listOfFiles[i].getName().equals(WSO2_LOG_FILE_NAME))
{
listOfWso2Files.add(new TimeStampWiseFile(logLocation, listOfFiles[i].getName()));
}
}
// No files to delete in this case.
if(listOfWso2Files.size()<=noOfBackupFilesInt)
{
return;
}
TimeStampWiseFile[] listOfWSo2FilesArray = new TimeStampWiseFile[listOfWso2Files.size()];
listOfWSo2FilesArray= listOfWso2Files.toArray(listOfWSo2FilesArray);
// We need in descending order so that the old files are arranged at the bottom of the stack.
Arrays.sort(listOfWSo2FilesArray, Collections.reverseOrder());
int index=0;
for (int i = 0; i < listOfWSo2FilesArray.length; i++)
{
TimeStampWiseFile timeStampWiseFile = listOfWSo2FilesArray[i];
if(++index > noOfBackupFilesInt)
{
String fileName = timeStampWiseFile.getName();
timeStampWiseFile.delete();
if(LOG.isInfoEnabled())
{
LOG.info("Removed File "+fileName);
}
}
}
}
public void destroy()
{
}
public void init(SynapseEnvironment synapseEnvironment)
{
this.synapseEnvironment = synapseEnvironment;
}
public String getLogLocation()
{
return logLocation;
}
public void setLogLocation(String logLocation)
{
this.logLocation = logLocation;
if(logLocation== null || logLocation.trim().equals(""))
{
if(LOG.isInfoEnabled())
{
LOG.info("No LOG location provided. Therefore using the default location of "+DEFAULT_LOG_FILE_LOCATION);
}
logLocation= DEFAULT_LOG_FILE_LOCATION;
}
}
public String getLogFileSizeInMbStr()
{
return logFileSizeInMbStr;
}
public void setLogFileSizeInMbStr(String logFileSizeInMbStr)
{
this.logFileSizeInMbStr = logFileSizeInMbStr;
if(logFileSizeInMbStr== null || logFileSizeInMbStr.trim().equals(""))
{
if(LOG.isInfoEnabled())
{
LOG.info("No LOG file size in MB. Therefore using the default size of "+DEFAULT_LOG_FILE_SIZE_IN_MB+" Mb");
}
logFileSizeInMb= DEFAULT_LOG_FILE_SIZE_IN_MB * MB_TO_BYTES;
}
else
{
try
{
logFileSizeInMb= Integer.parseInt(logFileSizeInMbStr) * MB_TO_BYTES;
}
catch(NumberFormatException e)
{
LOG.error("logFileSizeInMb is not proper. If the size is 20MB, provide 20 as the 2nd argument. Due to the exception"
+ "using the default size of "+DEFAULT_LOG_FILE_SIZE_IN_MB+" Mb");
logFileSizeInMb= DEFAULT_LOG_FILE_SIZE_IN_MB * MB_TO_BYTES;
}
}
}
public String getNoOfBackupFiles()
{
return noOfBackupFiles;
}
public void setNoOfBackupFiles(String noOfBackupFiles)
{
this.noOfBackupFiles = noOfBackupFiles;
try
{
noOfBackupFilesInt= Integer.parseInt(noOfBackupFiles) ;
}
catch(NumberFormatException e)
{
LOG.error("NoOfBackupFiles is not proper. Proper a proper integer value. Due to the exception"
+ "using the default number of "+DEFAULT_BACKUP_FILES_NUMBER);
noOfBackupFilesInt= DEFAULT_BACKUP_FILES_NUMBER;
}
}
}
는 이제 단지 마스터 세입자에 예정대로이 추가 코드를입니다. 자세한 내용은 https://docs.wso2.com/display/ESB480/Adding+and+Scheduling+Tasks을 참조하십시오.
<?xml version="1.0" encoding="UTF-8"?>
<task xmlns="http://ws.apache.org/ns/synapse"
name="LogFileBackupTask"
class="LogFileBackupTask"
group="synapse.simple.quartz">
<trigger cron="0 0/1 * * * ?"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="noOfBackupFiles"
value="20"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="logFileSizeInMbStr"
value="20"/>
<property xmlns:task="http://www.wso2.org/products/wso2commons/tasks"
name="logLocation"
value="repository/logs/"/>
</task>
WSO2 ESB 4.9.0에서 작동합니다. 4.8.1에서 테스트 할 수 없었습니다. 4.9.0으로 업그레이드 할 수 있습니까? –
http://stackoverflow.com/questions/25548546/wso2carbon-log-not-rolling-as-expected –
4.9.0으로 업그레이드하는 것 외에 다른 옵션이 있습니까? –