How to write rolling file to user.home directory

How is it possible to write the rolling files to the user.home directory.

I tried so far to set an environment variable to access the path in the properties-file of tinylog:

System.setProperty("tinylog.directory", PathHelper.getLoggingDir());

Result: 'tinylog.directory' could not be found in environment variables by tinylog.properties

Here how I wrote it in the tinylog.properties:

writerRollingFile.file     = ${tinylog.directory}/demo/logs/{date:yyyy-MM-dd}-log.{count}.txt
writerRollingFile.latest   = ${tinylog.directory}/demo/logs/latest.txt

Another approach was to setup tinylog by code:

Configuration.set("writerRollingFile.file", String.format("%1$s%2$s%3$s", PathHelper.getLoggingDir(), File.separator,  "{date:yyyy-MM-dd}-log.{count}.txt"));
Configuration.set("writerRollingFile.latest", String.valueOf(Paths.get(PathHelper.getLoggingDir() ,  "latest.txt")));

Result: writerRollingFile.file doesn't work because tinylog has problems to write the rolling file -> between Path and File writes an ";" -> C:\Users\MyUserName\demo\logs;2022-10-17-log.0.txt

The other file, "latest.txt", is created in right directory with no problems.

Here how I resolve the path with the method PathHelper.getLoggingDir():

Path pathToFile = Paths.get(sUserHomeDir, sAppName, sLoggingDir);

Would it possible to get it work with the user.home directory?

Thanks Martin, to replace the prefixes helped: ${...} with #{...}

My tiny.properties file:

# rolling file
writerRollingFile          = rolling file
writerRollingFile.level    = warn
writerRollingFile.format   = {date: dd.mm.yyyy HH:mm:ss.SSS} {level}: {class}.{method}()\t{message}
writerRollingFile.file     = #{tinylog.directory}/{date:yyyy-MM-dd}-log.{count}.txt
writerRollingFile.latest   = #{tinylog.directory}/latest.txt
writerRollingFile.charset  = UTF-8
writerRollingFile.buffered = true
writerRollingFile.policies = startup, daily: 00:00, size: 1mb
writerRollingFile.backups  = 100
writerRollingFile.convert  = gzip 

and to set the property at startup:

System.setProperty("tinylog.directory", PathHelper.getLoggingDir());

and to get or make the directory:

    private static final String sUserHomeDir = System.getProperty("user.home");
    private static final String sAppName =  "Demo";
    private static final String sLoggingDir =  "Logs";


    public static String getLoggingDir() throws IOException {

        // inserts correct file path separator on *nix and Windows
        Path pathToFile = Paths.get(sUserHomeDir, sAppName, sLoggingDir);

        // The Files.createDirectories creates a new directory;
        // if the parent directories do not exist, they are created as well.
        // The method does not thrown an exception if the directory already exist.
        Files.createDirectories(pathToFile);

        return pathToFile.toString();
   }


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations