I have two public methods that I'd like to trace. One of the methods calls the other repeatedly. What I'd like to do is trace only the method that was called from the outside.
Here's a simple class to demonstrate what I mean:
public class LoggingExample {
private static final Logger logger = LoggerFactory.getLogger(LoggingExample.class);
public static final String USER_ROOT = "/home/waisbrot";
/** could be called by fileExistsRobust *or* from outside */
public static boolean fileExists(String filename) {
logger.trace("Checking for file {}", filename);
File f = new File(filename);
return f.exists();
}
/** always gets called from outside */
public static boolean fileExistsRobust(String filename) {
logger.trace("Checking for any varient of {}", filename);
if (fileExists(filename))
return true;
for (String prefix : prefixes) { // this list is 100 items long
if (fileExists(prefix + filename));
return true;
}
return false;
}
}
Elsewhere in my code, I might call fileExists, in which case I want its logging message to get printed (assuming I'm tracing it). But if I call fileExistsRobost than I want that log message, but not fileExists.
I want to have both methods traced, but I'm getting buried in output when I call the second one. I was hoping Logback could be configured to understand what I want, but I'm not seeing anything useful in the documentation. I could flip a flag when I enter fileExistsRobust and then test for it in fileExists, but that's going to get ugly with more than one thread (since these are static methods) and it seems like it starts polluting the class with lots of logging infrastructure. I could use MDC to store the info, but that seems like an abuse of MDC.
Anyone run into this situation before? How'd you deal with it?
I assume that you are able to change the code. Then the simplest way in my opinion is avoiding the problem by introducing another internalFileExists(String filename) or overloading fileExists(String filename) with a logging toogle:
public static boolean fileExists(String filename, boolean doLog) {
if (doLog) logger.trace("Checking for file {}", filename);
File f = new File(filename);
return f.exists();
}
and let fileExistsRobust use the overloaded version with doLog = false, while the single argument version redirects to fileExists(filename, true).
That does not really address the problem, but mitigates it.
Related
How do you mock file reading/writing via JUnit?
Here is my scenario
MyHandler.java
public abstract class MyHandler {
private String path = //..path/to/file/here
public synchronized void writeToFile(String infoText) {
// Some processing
// Writing to File Here
File file = FileUtils.getFile(filepath);
file.createNewFile();
// file can't be written, throw FileWriteException
if (file.canWrite()) {
FileUtils.writeByteArrayToFile(file, infoText.getBytes(Charsets.UTF_8));
} else {
throw new FileWriteException();
}
}
public String readFromFile() {
// Reading from File here
String infoText = "";
File file = new File(path);
// file can't be read, throw FileReadException
if (file.canRead()) {
infoText = FileUtils.readFileToString(file, Charsets.UTF_8);
} else {
throw FileReadException();
}
return infoText
}
}
MyHandlerTest.java
#RunWith(PowerMockRunner.class)
#PrepareForTest({
MyHandler.class
})
public class MyHandlerTest {
private static MyHandler handler = null;
// Some Initialization for JUnit (i.e #Before, #BeforeClass, #After, etc)
#Test(expected = FileWriteException.class)
public void writeFileTest() throws Exception {
handler.writeToFile("Test Write!");
}
#Test(expected = FileReadException.class)
public void readFileTest() throws Exception {
handler.readFromFile();
}
}
Given above source, Scenario when file is not writable (write permission not allowed) is OK, However, when i try to do scenario wherein file is not readable (read permission not allowed). It always read the file, i have already tried to modify the file permission on the test code via below
File f = new File("..path/to/file/here");
f.setReadable(false);
However, I did some reading, setReadable() always returns false (failed) when run on Windows machine.
Is there a way to modify the file permission of the target file programmatically in relation to JUnit?
Note
Target source code to test cannot be modified, meaning
Myhandler.class is a legacy code which is not to be modified.
Instead of relying on the operating system file permissions, use PowerMock to mock FileUtils.getFile(...) and make it return an instance of File (e.g. anonymous sub class) that returns a specific value for canWrite()/canRead().
Mocking static methods with Mockito
Since Mockito cannot mock static methods, use a File factory instead (or refactor your FileUtils to be a factory), then you can mock it and return a mocked File instance as well, where you can also mock any File methods you want.
So instead of FileUtils.getFile(filepath) you will now have something like FileFactory.getInstance().getFile(filepath) for example, where you can mock getFile(String) method easily.
In jUnit there's a handy rule for scenarios like yours.
public class MyHandlerTest {
#Rule
// creates a temp folder that will be removed after each test
public org.junit.rules.TemporaryFolder folder = new org.junit.rules.TemporaryFolder();
private MyHandler handler;
#Before
public void setUp() throws Exception {
File file = folder.newFile("myFile.txt");
// do whatever you need with it - fill with test content and so on.
handler = new MyHandler(file.getAbsolutePath()); // use the real thing
}
// Test whatever behaviour you need with a real file and predefined dataset.
}
I need to include the PID in my log4js logs. I see many examples which use the thread context. However, these need to be set on each individual thread created. I am constrained against doing this.
I need a solution that, either, does not use the thread context, or, can set the PID on all thread contexts, for any thread that may be created, from any arbitrary class.
Please create a feature request on the Log4j2 issue tracker to make this a built-in feature.
For now, you can create a custom plugin. See code below. This will allow you to specify %pid in the pattern layout (similar to %m for the message).
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
#Plugin(name = "ProcessIdPatternConverter", category = "Converter")
#ConverterKeys({ "pid", "processId" })
public final class ProcessIdPatternConverter extends LogEventPatternConverter {
private final String pid;
private ProcessIdPatternConverter(String[] options) {
super("Process ID", "pid");
String temp = options.length > 0 ? options[0] : "???";
try {
// likely works on most platforms
temp = ManagementFactory.getRuntimeMXBean().getName().split("#")[0];
} catch (final Exception ex) {
try {
// try a Linux-specific way
temp = new File("/proc/self").getCanonicalFile().getName();
} catch (final IOException ignoredUseDefault) {}
}
pid = temp;
}
/**
* Obtains an instance of ProcessIdPatternConverter.
*
* #param options users may specify a default like {#code %pid{NOPID} }
* #return instance of ProcessIdPatternConverter.
*/
public static ProcessIdPatternConverter newInstance(final String[] options) {
return new ProcessIdPatternConverter(options);
}
#Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
toAppendTo.append(pid);
}
}
See the manual for more details on how Log4j2 plugins work.
One way to let Log4j2 recognize your plugin is by specifying the package name of the plugin class in the packages attribute of the configuration:
<Configuration status="trace"
packages="com.myorg.mypluginpackage">
(Trace switches on Log4j2 internal debugging to help with troubleshooting.)
I'm trying to rename an existing file using File#renameTo(), but it doesn't seem to work.
The following code represents what I am trying to do:
public class RenameFileDirectory {
public static void main(String[] args) throws IOException {
new RenameFileDirectory();
}
public RenameFileDirectory() throws IOException {
File file = new File("C:\\Users\\User-PC\\Desktop\\Nouveau dossier2\\file.png");
File desFile = new File ("C:\\Users\\User-PC\\Desktop\\Nouveau dossier2\\file2.png");
if (file.renameTo(desFile)) {
System.out.println("successful rename");
} else {
System.out.println("error");
}
}
}
Try using Files.move instead. If you read the javadocs for renameTo, it states that:
Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.
Is it possible to force Properties not to add the date comment in front? I mean something like the first line here:
#Thu May 26 09:43:52 CEST 2011
main=pkg.ClientMain
args=myargs
I would like to get rid of it altogether. I need my config files to be diff-identical unless there is a meaningful change.
Guess not. This timestamp is printed in private method on Properties and there is no property to control that behaviour.
Only idea that comes to my mind: subclass Properties, overwrite store and copy/paste the content of the store0 method so that the date comment will not be printed.
Or - provide a custom BufferedWriter that prints all but the first line (which will fail if you add real comments, because custom comments are printed before the timestamp...)
Given the source code or Properties, no, it's not possible. BTW, since Properties is in fact a hash table and since its keys are thus not sorted, you can't rely on the properties to be always in the same order anyway.
I would use a custom algorithm to store the properties if I had this requirement. Use the source code of Properties as a starter.
Based on https://stackoverflow.com/a/6184414/242042 here is the implementation I have written that strips out the first line and sorts the keys.
public class CleanProperties extends Properties {
private static class StripFirstLineStream extends FilterOutputStream {
private boolean firstlineseen = false;
public StripFirstLineStream(final OutputStream out) {
super(out);
}
#Override
public void write(final int b) throws IOException {
if (firstlineseen) {
super.write(b);
} else if (b == '\n') {
firstlineseen = true;
}
}
}
private static final long serialVersionUID = 7567765340218227372L;
#Override
public synchronized Enumeration<Object> keys() {
return Collections.enumeration(new TreeSet<>(super.keySet()));
}
#Override
public void store(final OutputStream out, final String comments) throws IOException {
super.store(new StripFirstLineStream(out), null);
}
}
Cleaning looks like this
final Properties props = new CleanProperties();
try (final Reader inStream = Files.newBufferedReader(file, Charset.forName("ISO-8859-1"))) {
props.load(inStream);
} catch (final MalformedInputException mie) {
throw new IOException("Malformed on " + file, mie);
}
if (props.isEmpty()) {
Files.delete(file);
return;
}
try (final OutputStream os = Files.newOutputStream(file)) {
props.store(os, "");
}
if you try to modify in the give xxx.conf file it will be useful.
The write method used to skip the First line (#Thu May 26 09:43:52 CEST 2011) in the store method. The write method run till the end of the first line. after it will run normally.
public class CleanProperties extends Properties {
private static class StripFirstLineStream extends FilterOutputStream {
private boolean firstlineseen = false;
public StripFirstLineStream(final OutputStream out) {
super(out);
}
#Override
public void write(final int b) throws IOException {
if (firstlineseen) {
super.write(b);
} else if (b == '\n') {
// Used to go to next line if did use this line
// you will get the continues output from the give file
super.write('\n');
firstlineseen = true;
}
}
}
private static final long serialVersionUID = 7567765340218227372L;
#Override
public synchronized Enumeration<java.lang.Object> keys() {
return Collections.enumeration(new TreeSet<>(super.keySet()));
}
#Override
public void store(final OutputStream out, final String comments)
throws IOException {
super.store(new StripFirstLineStream(out), null);
}
}
Can you not just flag up in your application somewhere when a meaningful configuration change takes place and only write the file if that is set?
You might want to look into Commons Configuration which has a bit more flexibility when it comes to writing and reading things like properties files. In particular, it has methods which attempt to write the exact same properties file (including spacing, comments etc) as the existing properties file.
You can handle this question by following this Stack Overflow post to retain order:
Write in a standard order:
How can I write Java properties in a defined order?
Then write the properties to a string and remove the comments as needed. Finally write to a file.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
properties.store(baos,null);
String propertiesData = baos.toString(StandardCharsets.UTF_8.name());
propertiesData = propertiesData.replaceAll("^#.*(\r|\n)+",""); // remove all comments
FileUtils.writeStringToFile(fileTarget,propertiesData,StandardCharsets.UTF_8);
// you may want to validate the file is readable by reloading and doing tests to validate the expected number of keys matches
InputStream is = new FileInputStream(fileTarget);
Properties testResult = new Properties();
testResult.load(is);
I would like to record the logs of my Java application.
I have created this class:
public class Log {
final Logger logger = Logger.getLogger("DigiScope.log");
public static void main(String[] args) {
}
public Log(String message) {
try {
// Create an appending file handler
boolean append = true;
FileHandler handler = new FileHandler("my.log", append);
// Add to the desired logger
Logger logger = Logger.getLogger("com.mycompany");
logger.addHandler(handler);
logger.info(message);
} catch (IOException e) {
}
}
}
And for each button I have a code like that:
private void btnNewPatient ActionPerformed(java.awt.event.ActionEvent evt) {
Log a = new Log("New Patient created");
}
This code creates a log.txt, but records only the click on the first button, the others clicks on the others buttons are not record.
Can you help me?
Thank you.
It doesn't make much sense to create a proprietary logging wrapper in your app - java.util.logging is already such a wrapper, so I recommend using it directly. You should create logger objects in your classes, then log messages within handler methods something like this:
logger.info("New Patient created");
And you should use the same Logger instance throughout your class, instead of creating new instances all the time. The standard way is to create one static final instance per class.
It is also better to configure logging from a config file, not from code.
I recommend reading through the Java Logging Tutorial.
Péter Török and StriplingWarrior are right with their suggestion to use the Logging framework in the right way:
logger.info("New Patient created");
instead of creating a new Logger for each statement.
But even with your construction, you should have a logfile with all logging information.
For each invocation of the Log constructor, a new my.log.X file is created (X is a number). And from this point in time each log statement is logged in this file.
So if you invoke the constructor three times (with message: "first", "second", "third") then you should have the files: my.log, my.log.1. my.log.2
my.log: "first", "second", "third"
my.log.1: "second", "third"
my.log.2: "third"
I'm guessing that it's probably logging each click, but you're reopening the file with each new log message, rather than appending to it.