001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.io.log;
020
021import java.io.PrintStream;
022
023import lucee.commons.io.log.log4j.LogAdapter;
024import lucee.commons.lang.ExceptionUtil;
025
026/**
027 * Helper class for the logs
028 */
029public final class LogUtil {
030        
031        public static final int LEVEL_TRACE=5; // FUTURE add to Log interface, if log interface not get removed
032    
033
034        public static void log(Log log, int level, String logName, Throwable t) { 
035                log(log,level,logName,"",t);
036        }   
037
038        public static void log(Log log, int level, String logName,String msg, Throwable t) { 
039                if(log instanceof LogAdapter) {
040                        ((LogAdapter)log).log(level, logName, msg,t);
041                }
042                else {
043                        String em = ExceptionUtil.getMessage(t);
044                        String est = ExceptionUtil.getStacktrace(t, false);
045                        if(msg.equals(em)) msg=em+";"+est;
046                        else msg+=";"+em+";"+est;
047                        
048                        if(log!=null) {
049                                log.log(level, logName,msg);
050                        }
051                        else {
052                                PrintStream ps=(level>=Log.LEVEL_WARN)?System.err:System.out;
053                                ps.println(logName+";"+msg);
054                        }
055
056                }
057        }
058
059        public static void log(Log log, int level, String logName, String msg, StackTraceElement[] stackTrace) {
060                Throwable t = new Throwable();
061                t.setStackTrace(stackTrace);
062                log(log,level,logName,msg,t);
063        }    
064        
065}