001/*
002 * $Id: Logger.java 4847 2011-05-05 19:46:13Z redlab_b $
003 *
004 * This file is part of the iText (R) project. Copyright (c) 1998-2011 1T3XT
005 * BVBA Authors: Balder Van Camp, Emiel Ackermann, et al.
006 *
007 * This program is free software; you can redistribute it and/or modify it under
008 * the terms of the GNU Affero General License version 3 as published by the
009 * Free Software Foundation with the addition of the following permission added
010 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
011 * IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT, 1T3XT DISCLAIMS THE WARRANTY OF NON
012 * INFRINGEMENT OF THIRD PARTY RIGHTS.
013 *
014 * This program is distributed in the hope that it will be useful, but WITHOUT
015 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
016 * FOR A PARTICULAR PURPOSE. See the GNU Affero General License for more
017 * details. You should have received a copy of the GNU Affero General License
018 * along with this program; if not, see http://www.gnu.org/licenses or write to
019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
020 * MA, 02110-1301 USA, or download the license from the following URL:
021 * http://itextpdf.com/terms-of-use/
022 *
023 * The interactive user interfaces in modified source and object code versions
024 * of this program must display Appropriate Legal Notices, as required under
025 * Section 5 of the GNU Affero General License.
026 *
027 * In accordance with Section 7(b) of the GNU Affero General License, a covered
028 * work must retain the producer line in every PDF that is created or
029 * manipulated using iText.
030 *
031 * You can be released from the requirements of the license by purchasing a
032 * commercial license. Buying such a license is mandatory as soon as you develop
033 * commercial activities involving the iText software without disclosing the
034 * source code of your own applications. These activities include: offering paid
035 * services to customers as an ASP, serving PDFs on the fly in a web
036 * application, shipping iText with a closed source product.
037 *
038 * For more information, please contact iText Software Corp. at this address:
039 * sales@itextpdf.com
040 */
041package com.itextpdf.text.log;
042
043
044/**
045 * A Simple System.out logger.
046 * @author redlab_be
047 *
048 */
049public class SysoLogger implements Logger {
050
051        private String name;
052        private final int shorten;
053
054        /**
055         * Defaults packageReduce to 1.
056         */
057        public SysoLogger() {
058                this(1);
059        }
060        /**
061         * Amount of characters each package name should be reduced with.
062         * @param packageReduce
063         *
064         */
065        public SysoLogger(final int packageReduce) {
066                this.shorten = packageReduce;
067        }
068
069        /**
070         * @param klass
071         * @param shorten
072         */
073        protected SysoLogger(final String klass, final int shorten) {
074                this.shorten = shorten;
075                this.name = klass;
076        }
077
078        public Logger getLogger(final Class<?> klass) {
079                return new SysoLogger(klass.getName(), shorten);
080        }
081
082        /* (non-Javadoc)
083         * @see com.itextpdf.text.log.Logger#getLogger(java.lang.String)
084         */
085        public Logger getLogger(final String name) {
086                return new SysoLogger("[itext]", 0);
087        }
088
089        public boolean isLogging(final Level level) {
090                return true;
091        }
092
093        public void warn(final String message) {
094                System.out.println(String.format("%s WARN  %s", shorten(name), message));
095        }
096
097        /**
098         * @param name2
099         * @return
100         */
101        private String shorten(final String className) {
102                if (shorten != 0) {
103                        StringBuilder target = new StringBuilder();
104                        String name = className;
105                        int fromIndex = className.indexOf('.');
106                        while (fromIndex != -1) {
107                                int parseTo = (fromIndex < shorten) ? (fromIndex) : (shorten);
108                                target.append(name.substring(0, parseTo));
109                                target.append('.');
110                                name = name.substring(fromIndex + 1);
111                                fromIndex = name.indexOf('.');
112                        }
113                        target.append(className.substring(className.lastIndexOf('.') + 1));
114                        return target.toString();
115                }
116                return className;
117        }
118
119        public void trace(final String message) {
120                System.out.println(String.format("%s TRACE %s", shorten(name), message));
121        }
122
123        public void debug(final String message) {
124                System.out.println(String.format("%s DEBUG %s", shorten(name), message));
125        }
126
127        public void info(final String message) {
128                System.out.println(String.format("%s INFO  %s", shorten(name), message));
129        }
130
131        public void error(final String message) {
132                System.out.println(String.format("%s ERROR %s", name, message));
133        }
134
135        public void error(final String message, final Exception e) {
136                System.out.println(String.format("%s ERROR %s", name, message));
137                e.printStackTrace(System.out);
138        }
139}