Change logging to include error log

Got an idea? Missing something? Post your feature request here.

Moderator: moderators

Change logging to include error log

Postby baaldemon » Wed Dec 15, 2010 5:34 pm

I would like to request a secondary logfile to capture the exception stack traces, rather than having those caputred in the standard logfile.

Im not sure if Im the only person who likes to routinely go through the log file and mine it for information. But to me the stack traces dont belong in the standard logfile. So Ive modified my code to write out to two different logs, the first being the standard logfile logs all messages but does not print out the full stack traces for exceptions thrown in the system, the second logging only those entries with stack trace. Im not sure if this would be helpful to others, but thought Id see if this was something you would like to see in the codebase.

Here are the changes:
Logger Class
Code: Select all
    private static PrintWriter writer, warner;
    private static String logFile  = "subsonic.log";
    private static String errorLog = "subsonic.err";
    private static final char LOG = 'l';
    private static final char ERR = 'e';
    ...
    ...
    ...
    private void add(Level level, Object message, Throwable error) {
        Entry entry = new Entry(category, level, message, error);
        try {
            getPrintWriter().println(entry);
            if (error != null) {
                getPrintWriter(ERR).println(entry.stringForErr());
            }
        } catch (IOException x) {
            System.err.println("Failed to write to subsonic.log.");
            x.printStackTrace();
        }
        entries.add(entry);
    }

    private static synchronized PrintWriter getPrintWriter() throws IOException {
//        if (writer == null) {
//            writer = new PrintWriter(new FileWriter(getLogFile(), true), true);
//        }
//        return writer;
        return getPrintWriter(LOG);
    }

    private static synchronized PrintWriter getPrintWriter(char type) throws IOException {
        PrintWriter ret = null;
        if (type == LOG) {
            ret = ((writer == null) ? new PrintWriter(new FileWriter(getLogFile(), true), true) : writer);
        }
        else if (type == ERR) {
            ret = ((warner == null) ? new PrintWriter(new FileWriter(getWarnFile(), true), true) : warner);
        }
        return ret;
    }

    public static File getLogFile() {
        File subsonicHome = SettingsService.getSubsonicHome();
        return new File(subsonicHome, logFile);
    }

    public static File getWarnFile() {
        File subsonicHome = SettingsService.getSubsonicHome();
        return new File(subsonicHome, errorLog);
    }



Entry Class Changes:
Code: Select all
        public String toString() {
            StringBuffer buf = new StringBuffer();
            buf.append('[').append(DATE_FORMAT.format(date)).append("] ");
            buf.append(level).append(' ');
            buf.append(category).append(" - ");
            buf.append(message);

            if (error != null) {
               // buf.append('\n').append(ExceptionUtils.getFullStackTrace(error));
               buf.append(" - see "+errorLog+" for full details");
            }

            return buf.toString();
        }
        public String stringForErr() {
            String str = this.toString();
            if (error != null) {
                str += "\n"+ExceptionUtils.getFullStackTrace(error);
            }
            return str;
        }

Code: Select all
baaldemon
 
Posts: 99
Joined: Fri May 07, 2010 11:54 am

Return to Feature Requests

Who is online

Users browsing this forum: No registered users and 7 guests