com.archivas.logging.FileHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.archivas.logging.FileHandler.java

Source

// Copyright 2007 Hitachi Data Systems
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

package com.archivas.logging;

import java.io.IOException;
import java.util.logging.LogManager;

import com.archivas.clienttools.arcutils.utils.FileUtil;
import org.apache.commons.lang.text.StrBuilder;

/**
 * This class extends {@link java.util.logging.FileHandler}, parses additional configuration
 * parameters:
 *
 * <ul>
 * <li>"%a" the value of System.getProperty("application_home_dir");
 * </ul>
 *
 */
public class FileHandler extends java.util.logging.FileHandler {

    private static String getPattern() {
        LogManager manager = LogManager.getLogManager();

        String cname = FileHandler.class.getName();

        String pattern = manager.getProperty(cname + ".pattern");
        if (pattern == null) {
            pattern = "%h/java%u.log";
        }

        return pattern;
    }

    private static int getLimit() {
        LogManager manager = LogManager.getLogManager();
        String cname = FileHandler.class.getName();

        String limitStr = manager.getProperty(cname + ".limit");
        int limit = 10000000;
        if (limitStr != null) {
            limit = Integer.valueOf(limitStr);
        }

        return limit;
    }

    private static int getCount() {
        LogManager manager = LogManager.getLogManager();
        String cname = FileHandler.class.getName();

        String countStr = manager.getProperty(cname + ".count");
        int count = 9;
        if (countStr != null) {
            count = Integer.valueOf(countStr);
        }

        return count;
    }

    private static boolean getAppend() {
        LogManager manager = LogManager.getLogManager();
        String cname = FileHandler.class.getName();

        String appendStr = manager.getProperty(cname + ".append");
        boolean append = true;
        if (appendStr != null) {
            append = Boolean.valueOf(appendStr);
        }

        return append;
    }

    public FileHandler() throws IOException, SecurityException {
        super(buildPattern(getPattern()), getLimit(), getCount(), getAppend());
    }

    public FileHandler(final String pattern) throws IOException, SecurityException {
        super(buildPattern(pattern));
    }

    public FileHandler(final String pattern, final boolean append) throws IOException, SecurityException {
        super(buildPattern(pattern), append);
    }

    public FileHandler(final String pattern, final int limit, final int count)
            throws IOException, SecurityException {
        super(buildPattern(pattern), limit, count);
    }

    public FileHandler(final String pattern, final int limit, final int count, final boolean append)
            throws IOException, SecurityException {
        super(buildPattern(pattern), limit, count, append);
    }

    protected static String buildPattern(String pattern) {
        String retval;
        if (pattern == null) {
            return null;
        }

        StrBuilder newPattern = new StrBuilder(pattern);
        String applicationHomeDir = System.getProperty("application_home_dir");
        String userHomeDir = System.getProperty("user.home");
        String tmpDir = System.getProperty("java.io.tmpdir");
        if (tmpDir == null) {
            tmpDir = userHomeDir;
        }

        if (applicationHomeDir != null) {
            newPattern = newPattern.replaceAll("%a", applicationHomeDir);
        }
        retval = newPattern.toString();

        // ensure that the log directory exists
        // Replace the known variables that are common in directories so we can create the dir.
        newPattern = newPattern.replaceAll("%t", tmpDir);
        newPattern = newPattern.replaceAll("%h", userHomeDir);

        String logPath = FileUtil.getPath(newPattern.toString());
        FileUtil.mkdirs(logPath);
        return retval;
    }
}