jPOS icon indicating copy to clipboard operation
jPOS copied to clipboard

Log folder does not exist, causing silent failure at start up

Open mchhil-incomm opened this issue 1 year ago • 4 comments

If the logName is like xyx/a.log and the xyz folder does not exist a FileNotFound exception is thrown and since the log file is not there it gets lost.

https://github.com/jpos/jPOS/blob/master/jpos/src/main/java/org/jpos/util/RotateLogListener.java#L154-L161

I have made a local code change to create the path directories

    protected synchronized void openLogFile() throws IOException {
        if (f != null) {
            f.close();
        }

        Path filePath = Paths.get(logName);
        if (!(filePath.toFile()
                      .exists())) {
            // Ensure the parent directories exist
            Files.createDirectories(filePath.getParent());
            // Create the file if it does not exist
            Files.createFile(filePath);

        }

        f = new FileOutputStream(filePath.toFile(), true);


     setPrintStream (new PrintStream(f)); 
     p.println ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 
     p.println ("<logger class=\"" + getClass().getName() + "\">"); 
    }

And a unit test for my environment where the logger deploy has the appropriate prefix suffix

  <log-listener class="org.jpos.util.DailyLogListener">
    <property name="prefix" value="xyx/a" />
    <property name="suffix" value=".log" />
    <property name="window" value="86400" />
    <property name="copies" value="90" />
    <property name="maxsize" value="100000000" />
    <property name="compression-format" value="none" />
    <property name="rotate-on-startup" value="true" />
  </log-listener>
    public void initSwitchTest() throws InterruptedException, IOException {

        Q2 q2 = new Q2("deploy");
        q2.start();

        Awaitility.await().atMost(10, SECONDS).until(q2::running);
        Thread.sleep(5000);

        q2.shutdown();
        Assert.assertTrue(new File("xyx/a.log").exists());
    }

mchhil-incomm avatar Jul 16 '24 06:07 mchhil-incomm