+
Skip to content

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

@mchhil-incomm

Description

@mchhil-incomm

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());
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载