class LoggingService {

    public static void setupMicroserviceLogger(String microserviceUuid, long logSize) throws IOException {
        int maxFileSize = (int) (logSize * 1_000_000);
        int logFileCount = Math.round(logSize);
        final File logDirectory = new File(Configuration.getLogDiskDirectory());

        logDirectory.mkdirs();

        UserPrincipalLookupService lookupservice = FileSystems.getDefault().getUserPrincipalLookupService();
        final GroupPrincipal group = lookupservice.lookupPrincipalByGroupName(Constants.OS_GROUP);
        if (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) {
            PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(logDirectory.toPath(), PosixFileAttributeView.class,
                    LinkOption.NOFOLLOW_LINKS);
            fileAttributeView.setGroup(group);

            Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxrwx---");
            Files.setPosixFilePermissions(logDirectory.toPath(), perms);

        } else if (SystemUtils.IS_OS_WINDOWS) {
            DosFileAttributeView fileAttributeView = Files.getFileAttributeView(logDirectory.toPath(), DosFileAttributeView.class,
                    LinkOption.NOFOLLOW_LINKS);
            fileAttributeView.setReadOnly(false);

            File dir = logDirectory.toPath().toFile();
            dir.setReadable(true, false);
            dir.setExecutable(true, false);
            dir.setWritable(true, false);
        }


        final String logFilePattern = logDirectory.getPath() + "/" + microserviceUuid + ".%g.log";
        Logger logger = microserviceLogger.get(microserviceUuid);

        if (logger != null) {
            for (Handler f : logger.getHandlers())
                f.close();
        }

        if (logFileCount == 0) {
            logFileCount = 1;
        }

        Handler logFileHandler = new FileHandler(logFilePattern, maxFileSize / logFileCount, logFileCount);

        logFileHandler.setFormatter(new LogFormatter());

        logger = Logger.getLogger(microserviceUuid);
        logger.addHandler(logFileHandler);

        logger.setUseParentHandlers(false);

        microserviceLogger.put(microserviceUuid, logger);
    }

    private  LoggingService();

}