com.loadcoder.logback.SharedDirFileAppenderLogback Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (C) 2018 Stefan Vahlgren at Loadcoder
*
* This file is part of Loadcoder.
*
* Loadcoder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Loadcoder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
******************************************************************************/
package com.loadcoder.logback;
import java.io.File;
import java.io.IOException;
import com.loadcoder.result.Logs;
import com.loadcoder.result.SharedDirFileAppender;
import ch.qos.logback.core.FileAppender;
public class SharedDirFileAppenderLogback extends FileAppender implements SharedDirFileAppender{
/**
* An extension of FileAppender. The purpose of this class is to have a fileappender with the ability
* to change the filepath for it and every other instances of its type at once.
* This is useful when executing multiple tests where the logs of each test must be separated
*/
String nextFileName = null;
public SharedDirFileAppenderLogback(){
synchronized(Logs.sharedDirFileAppenders) {
Logs.sharedDirFileAppenders.add(this);
}
}
public void setNextFileName(String nextFileName) {
this.nextFileName = nextFileName;
}
public void changeToDir(File newDir) throws IOException{
String filename = getFile();
File file = new File(filename);
String nameOfTheFile = file.getName();
if(nextFileName != null){
nameOfTheFile = nextFileName;
}
String newFile = newDir + "/" + nameOfTheFile;
openFile(newFile);
}
}