System.out
or
32 | * System.err
using a layout specified by the user. The
33 | * default target is System.out
.
34 | *
35 | * ElasticSearch: Adapter from log4j to allow to disable console logging...
36 | * 37 | * @author Ceki Gülcü 38 | * @author Curt Arnold 39 | * @since 1.1 40 | */ 41 | public class ConsoleAppender extends WriterAppender { 42 | 43 | public static final String SYSTEM_OUT = "System.out"; 44 | public static final String SYSTEM_ERR = "System.err"; 45 | 46 | protected String target = SYSTEM_OUT; 47 | 48 | /** 49 | * Determines if the appender honors reassignments of System.out 50 | * or System.err made after configuration. 51 | */ 52 | private boolean follow = true; 53 | 54 | /** 55 | * Constructs an unconfigured appender. 56 | */ 57 | public ConsoleAppender() { 58 | } 59 | 60 | /** 61 | * Creates a configured appender. 62 | * 63 | * @param layout layout, may not be null. 64 | */ 65 | public ConsoleAppender(Layout layout) { 66 | this(layout, SYSTEM_OUT); 67 | } 68 | 69 | /** 70 | * Creates a configured appender. 71 | * 72 | * @param layout layout, may not be null. 73 | * @param target target, either "System.err" or "System.out". 74 | */ 75 | public ConsoleAppender(Layout layout, String target) { 76 | setLayout(layout); 77 | setTarget(target); 78 | activateOptions(); 79 | } 80 | 81 | /** 82 | * Sets the value of the Target option. Recognized values 83 | * are "System.out" and "System.err". Any other value will be 84 | * ignored. 85 | */ 86 | public void setTarget(String value) { 87 | String v = value.trim(); 88 | 89 | if (SYSTEM_OUT.equalsIgnoreCase(v)) { 90 | target = SYSTEM_OUT; 91 | } else if (SYSTEM_ERR.equalsIgnoreCase(v)) { 92 | target = SYSTEM_ERR; 93 | } else { 94 | targetWarn(value); 95 | } 96 | } 97 | 98 | /** 99 | * Returns the current value of the Target property. The 100 | * default value of the option is "System.out". 101 | * 102 | * See also {@link #setTarget}. 103 | */ 104 | public String getTarget() { 105 | return target; 106 | } 107 | 108 | /** 109 | * Sets whether the appender honors reassignments of System.out 110 | * or System.err made after configuration. 111 | * 112 | * @param newValue if true, appender will use value of System.out or 113 | * System.err in force at the time when logging events are appended. 114 | * @since 1.2.13 115 | */ 116 | public final void setFollow(final boolean newValue) { 117 | follow = newValue; 118 | } 119 | 120 | /** 121 | * Gets whether the appender honors reassignments of System.out 122 | * or System.err made after configuration. 123 | * 124 | * @return true if appender will use value of System.out or 125 | * System.err in force at the time when logging events are appended. 126 | * @since 1.2.13 127 | */ 128 | public final boolean getFollow() { 129 | return follow; 130 | } 131 | 132 | void targetWarn(String val) { 133 | LogLog.warn("[" + val + "] should be System.out or System.err."); 134 | LogLog.warn("Using previously set target, System.out by default."); 135 | } 136 | 137 | /** 138 | * Prepares the appender for use. 139 | */ 140 | public void activateOptions() { 141 | if (follow) { 142 | if (target.equals(SYSTEM_ERR)) { 143 | setWriter(createWriter(new SystemErrStream())); 144 | } else { 145 | setWriter(createWriter(new SystemOutStream())); 146 | } 147 | } else { 148 | if (target.equals(SYSTEM_ERR)) { 149 | setWriter(createWriter(System.err)); 150 | } else { 151 | setWriter(createWriter(System.out)); 152 | } 153 | } 154 | 155 | super.activateOptions(); 156 | } 157 | 158 | /** 159 | * {@inheritDoc} 160 | */ 161 | protected 162 | final void closeWriter() { 163 | if (follow) { 164 | super.closeWriter(); 165 | } 166 | } 167 | 168 | 169 | /** 170 | * An implementation of OutputStream that redirects to the 171 | * current System.err. 172 | */ 173 | private static class SystemErrStream extends OutputStream { 174 | public SystemErrStream() { 175 | } 176 | 177 | public void close() { 178 | } 179 | 180 | public void flush() { 181 | System.err.flush(); 182 | } 183 | 184 | public void write(final byte[] b) throws IOException { 185 | if (!Loggers.consoleLoggingEnabled()) { 186 | return; 187 | } 188 | System.err.write(b); 189 | } 190 | 191 | public void write(final byte[] b, final int off, final int len) 192 | throws IOException { 193 | if (!Loggers.consoleLoggingEnabled()) { 194 | return; 195 | } 196 | System.err.write(b, off, len); 197 | } 198 | 199 | public void write(final int b) throws IOException { 200 | if (!Loggers.consoleLoggingEnabled()) { 201 | return; 202 | } 203 | System.err.write(b); 204 | } 205 | } 206 | 207 | /** 208 | * An implementation of OutputStream that redirects to the 209 | * current System.out. 210 | */ 211 | private static class SystemOutStream extends OutputStream { 212 | public SystemOutStream() { 213 | } 214 | 215 | public void close() { 216 | } 217 | 218 | public void flush() { 219 | System.out.flush(); 220 | } 221 | 222 | public void write(final byte[] b) throws IOException { 223 | if (!Loggers.consoleLoggingEnabled()) { 224 | return; 225 | } 226 | System.out.write(b); 227 | } 228 | 229 | public void write(final byte[] b, final int off, final int len) 230 | throws IOException { 231 | if (!Loggers.consoleLoggingEnabled()) { 232 | return; 233 | } 234 | System.out.write(b, off, len); 235 | } 236 | 237 | public void write(final int b) throws IOException { 238 | if (!Loggers.consoleLoggingEnabled()) { 239 | return; 240 | } 241 | System.out.write(b); 242 | } 243 | } 244 | 245 | } 246 | -------------------------------------------------------------------------------- /src/main/java/net/csdn/common/logging/log4j/JLinePatternLayout.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Licensed to Elastic Search and Shay Banon under one 3 | * or more contributor license agreements. See the NOTICE file 4 | * distributed with this work for additional information 5 | * regarding copyright ownership. Elastic Search licenses this 6 | * file to you under the Apache License, Version 2.0 (the 7 | * "License"); you may not use this file except in compliance 8 | * with the License. You may obtain a copy of the License at 9 | * 10 | * http://www.apache.org/licenses/LICENSE-2.0 11 | * 12 | * Unless required by applicable law or agreed to in writing, 13 | * software distributed under the License is distributed on an 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 | * KIND, either express or implied. See the License for the 16 | * specific language governing permissions and limitations 17 | * under the License. 18 | */ 19 | 20 | package net.csdn.common.logging.log4j; 21 | 22 | import net.csdn.common.jline.ANSI; 23 | import org.apache.log4j.Level; 24 | import org.apache.log4j.PatternLayout; 25 | import org.apache.log4j.helpers.FormattingInfo; 26 | import org.apache.log4j.helpers.PatternConverter; 27 | import org.apache.log4j.helpers.PatternParser; 28 | import org.apache.log4j.spi.LoggingEvent; 29 | 30 | import java.lang.reflect.Field; 31 | 32 | import static jline.ANSIBuffer.ANSICodes.attrib; 33 | import static net.csdn.common.jline.ANSI.Code.*; 34 | 35 | /** 36 | * @author kimchy (Shay Banon) 37 | */ 38 | public class JLinePatternLayout extends PatternLayout { 39 | 40 | @Override 41 | protected PatternParser createPatternParser(String pattern) { 42 | try { 43 | return new JLinePatternParser(pattern); 44 | } catch (Throwable t) { 45 | return super.createPatternParser(pattern); 46 | } 47 | } 48 | 49 | private final static class JLinePatternParser extends PatternParser { 50 | 51 | private JLinePatternParser(String pattern) { 52 | super(pattern); 53 | } 54 | 55 | @Override 56 | protected void addConverter(PatternConverter pc) { 57 | try { 58 | if (ANSI.isEnabled()) { 59 | if (pc.getClass().getName().endsWith("BasicPatternConverter")) { 60 | Field typeField = pc.getClass().getDeclaredField("type"); 61 | typeField.setAccessible(true); 62 | Integer type = (Integer) typeField.get(pc); 63 | if (type == 2002) { 64 | pc = new ColoredLevelPatternConverter(formattingInfo); 65 | } 66 | } 67 | } 68 | } catch (Throwable t) { 69 | // ignore 70 | } 71 | super.addConverter(pc); 72 | } 73 | 74 | private static class ColoredLevelPatternConverter extends PatternConverter { 75 | 76 | ColoredLevelPatternConverter(FormattingInfo formattingInfo) { 77 | super(formattingInfo); 78 | } 79 | 80 | public String convert(LoggingEvent event) { 81 | if (!ANSI.isEnabled()) { 82 | return event.getLevel().toString(); 83 | } 84 | if (event.getLevel() == Level.FATAL) { 85 | return attrib(FG_RED) + event.getLevel().toString() + attrib(OFF); 86 | } else if (event.getLevel() == Level.ERROR) { 87 | return attrib(FG_RED) + event.getLevel().toString() + attrib(OFF); 88 | } else if (event.getLevel() == Level.WARN) { 89 | return attrib(FG_YELLOW) + event.getLevel().toString() + ' ' + attrib(OFF); 90 | } else if (event.getLevel() == Level.INFO) { 91 | return attrib(FG_GREEN) + event.getLevel().toString() + ' ' + attrib(OFF); 92 | } else if (event.getLevel() == Level.DEBUG) { 93 | return attrib(FG_CYAN) + event.getLevel().toString() + attrib(OFF); 94 | } else if (event.getLevel() == Level.TRACE) { 95 | return attrib(FG_BLUE) + event.getLevel().toString() + attrib(OFF); 96 | } 97 | return event.getLevel().toString(); 98 | } 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/main/java/net/csdn/common/logging/log4j/Log4jCSLogger.java: -------------------------------------------------------------------------------- 1 | package net.csdn.common.logging.log4j; 2 | 3 | import net.csdn.common.logging.support.AbstractCSLogger; 4 | import org.apache.log4j.Level; 5 | import org.apache.log4j.Logger; 6 | 7 | /** 8 | * BlogInfo: william 9 | * Date: 11-9-1 10 | * Time: 下午3:24 11 | */ 12 | public class Log4jCSLogger extends AbstractCSLogger { 13 | private final org.apache.log4j.Logger logger; 14 | 15 | public Log4jCSLogger(String prefix, Logger logger) { 16 | super(prefix); 17 | this.logger = logger; 18 | } 19 | 20 | @Override 21 | public String getName() { 22 | return logger.getName(); 23 | } 24 | 25 | @Override 26 | public boolean isTraceEnabled() { 27 | return logger.isTraceEnabled(); 28 | } 29 | 30 | @Override 31 | public boolean isDebugEnabled() { 32 | return logger.isDebugEnabled(); 33 | } 34 | 35 | @Override 36 | public boolean isInfoEnabled() { 37 | return logger.isInfoEnabled(); 38 | } 39 | 40 | @Override 41 | public boolean isWarnEnabled() { 42 | return logger.isEnabledFor(Level.WARN); 43 | } 44 | 45 | @Override 46 | public boolean isErrorEnabled() { 47 | return logger.isEnabledFor(Level.ERROR); 48 | } 49 | 50 | @Override 51 | public boolean isHadooEnabled() { 52 | return logger.isEnabledFor(Log4jFactory.CSLogLevel.HADOO_LEVEL); 53 | } 54 | 55 | @Override 56 | protected void internalHadoo(String msg) { 57 | logger.log(Log4jFactory.CSLogLevel.HADOO_LEVEL, msg); 58 | } 59 | 60 | @Override 61 | protected void internalHadoo(String msg, Throwable cause) { 62 | logger.log(Log4jFactory.CSLogLevel.HADOO_LEVEL, msg, cause); 63 | } 64 | 65 | @Override 66 | protected void internalTrace(String msg) { 67 | logger.trace(msg); 68 | } 69 | 70 | @Override 71 | protected void internalTrace(String msg, Throwable cause) { 72 | logger.trace(msg, cause); 73 | } 74 | 75 | @Override 76 | protected void internalDebug(String msg) { 77 | logger.debug(msg); 78 | } 79 | 80 | @Override 81 | protected void internalDebug(String msg, Throwable cause) { 82 | logger.debug(msg, cause); 83 | } 84 | 85 | @Override 86 | protected void internalInfo(String msg) { 87 | logger.info(msg); 88 | } 89 | 90 | @Override 91 | protected void internalInfo(String msg, Throwable cause) { 92 | logger.info(msg, cause); 93 | } 94 | 95 | @Override 96 | protected void internalWarn(String msg) { 97 | logger.warn(msg); 98 | } 99 | 100 | @Override 101 | protected void internalWarn(String msg, Throwable cause) { 102 | logger.warn(msg, cause); 103 | } 104 | 105 | @Override 106 | protected void internalError(String msg) { 107 | logger.error(msg); 108 | } 109 | 110 | @Override 111 | protected void internalError(String msg, Throwable cause) { 112 | logger.error(msg, cause); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/net/csdn/common/logging/log4j/Log4jFactory.java: -------------------------------------------------------------------------------- 1 | package net.csdn.common.logging.log4j; 2 | 3 | import net.csdn.common.logging.CSLogger; 4 | import net.csdn.common.logging.CSLoggerFactory; 5 | import org.apache.log4j.Level; 6 | import org.apache.log4j.Logger; 7 | import org.apache.log4j.Priority; 8 | import org.apache.log4j.net.SyslogAppender; 9 | 10 | /** 11 | * BlogInfo: william 12 | * Date: 11-9-1 13 | * Time: 下午3:44 14 | */ 15 | public class Log4jFactory extends CSLoggerFactory { 16 | @Override 17 | protected CSLogger newInstance(String prefix, String name) { 18 | final Logger logger = Logger.getLogger(name); 19 | return new Log4jCSLogger(prefix, logger); 20 | } 21 | 22 | private static class HADOOLevel extends Level { 23 | 24 | protected HADOOLevel(int level, String levelStr, int syslogEquivalent) { 25 | super(level, levelStr, syslogEquivalent); 26 | } 27 | } 28 | 29 | public interface CSLogLevel { 30 | public static final Level HADOO_LEVEL = new HADOOLevel(Priority.DEBUG_INT - 1, "HADOO", SyslogAppender.LOG_LOCAL0); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/net/csdn/common/logging/log4j/LogConfigurator.java: -------------------------------------------------------------------------------- 1 | package net.csdn.common.logging.log4j; 2 | 3 | import com.google.common.collect.ImmutableMap; 4 | import net.csdn.common.collect.MapBuilder; 5 | import net.csdn.common.env.Environment; 6 | import net.csdn.common.settings.ImmutableSettings; 7 | import net.csdn.common.settings.Settings; 8 | import org.apache.log4j.PropertyConfigurator; 9 | 10 | import java.util.Map; 11 | import java.util.Properties; 12 | 13 | import static net.csdn.common.settings.ImmutableSettings.settingsBuilder; 14 | 15 | /** 16 | * BlogInfo: william 17 | * Date: 11-9-1 18 | * Time: 下午2:11 19 | */ 20 | public class LogConfigurator { 21 | private static boolean loaded; 22 | 23 | private static ImmutableMapPropertyPlaceholderHelper
that uses the supplied prefix and suffix. Unresolvable
25 | * placeholders are ignored.
26 | *
27 | * @param placeholderPrefix the prefix that denotes the start of a placeholder.
28 | * @param placeholderSuffix the suffix that denotes the end of a placeholder.
29 | */
30 | public PropertyPlaceholder(String placeholderPrefix, String placeholderSuffix) {
31 | this(placeholderPrefix, placeholderSuffix, true);
32 | }
33 |
34 | /**
35 | * Creates a new PropertyPlaceholderHelper
that uses the supplied prefix and suffix.
36 | *
37 | * @param placeholderPrefix the prefix that denotes the start of a placeholder.
38 | * @param placeholderSuffix the suffix that denotes the end of a placeholder.
39 | * @param ignoreUnresolvablePlaceholders indicates whether unresolvable placeholders should be ignored
40 | * (true
) or cause an exception (false
).
41 | */
42 | public PropertyPlaceholder(String placeholderPrefix, String placeholderSuffix,
43 | boolean ignoreUnresolvablePlaceholders) {
44 | Preconditions.checkNotNull(placeholderPrefix, "Argument 'placeholderPrefix' must not be null.");
45 | Preconditions.checkNotNull(placeholderSuffix, "Argument 'placeholderSuffix' must not be null.");
46 | this.placeholderPrefix = placeholderPrefix;
47 | this.placeholderSuffix = placeholderSuffix;
48 | this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders;
49 | }
50 |
51 | /**
52 | * Replaces all placeholders of format ${name}
with the corresponding property from the supplied {@link
53 | * java.util.Properties}.
54 | *
55 | * @param value the value containing the placeholders to be replaced.
56 | * @param properties the Properties
to use for replacement.
57 | * @return the supplied value with placeholders replaced inline.
58 | */
59 | public String replacePlaceholders(String value, final Properties properties) {
60 | Preconditions.checkNotNull(properties, "Argument 'properties' must not be null.");
61 | return replacePlaceholders(value, new PlaceholderResolver() {
62 |
63 | public String resolvePlaceholder(String placeholderName) {
64 | return properties.getProperty(placeholderName);
65 | }
66 | });
67 | }
68 |
69 | /**
70 | * Replaces all placeholders of format ${name}
with the value returned from the supplied {@link
71 | * PlaceholderResolver}.
72 | *
73 | * @param value the value containing the placeholders to be replaced.
74 | * @param placeholderResolver the PlaceholderResolver
to use for replacement.
75 | * @return the supplied value with placeholders replaced inline.
76 | */
77 | public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) {
78 | Preconditions.checkNotNull(value, "Argument 'value' must not be null.");
79 | return parseStringValue(value, placeholderResolver, new HashSetnull
if no replacement is to be made.
164 | */
165 | String resolvePlaceholder(String placeholderName);
166 | }
167 | }
168 |
--------------------------------------------------------------------------------
/src/main/java/net/csdn/common/reflect/ReflectHelper.java:
--------------------------------------------------------------------------------
1 | package net.csdn.common.reflect;
2 |
3 | import com.google.common.collect.Lists;
4 | import net.csdn.common.exception.ExceptionHandler;
5 | import org.apache.commons.beanutils.MethodUtils;
6 |
7 | import java.lang.annotation.Annotation;
8 | import java.lang.reflect.Field;
9 | import java.lang.reflect.Method;
10 | import java.lang.reflect.Modifier;
11 | import java.util.HashSet;
12 | import java.util.List;
13 | import java.util.Set;
14 |
15 | import static net.csdn.common.collections.WowCollections.list;
16 |
17 | /**
18 | * User: WilliamZhu
19 | * Date: 12-7-12
20 | * Time: 下午4:48
21 | */
22 | public class ReflectHelper {
23 |
24 |
25 | public static ListIt will also automatically load a comma separated list under the settingPrefix and merge with 177 | * the numbered format. 178 | * 179 | * @param settingPrefix The setting prefix to load the array by 180 | * @return The setting array values 181 | * @throws SettingsException 182 | */ 183 | String[] getAsArray(String settingPrefix, String[] defaultArray) throws SettingsException; 184 | 185 | /** 186 | * The values associated with a setting prefix as an array. The settings array is in the format of: 187 | * settingPrefix.[index]. 188 | *
189 | *It will also automatically load a comma separated list under the settingPrefix and merge with
190 | * the numbered format.
191 | *
192 | * @param settingPrefix The setting prefix to load the array by
193 | * @return The setting array values
194 | * @throws SettingsException
195 | */
196 | String[] getAsArray(String settingPrefix) throws SettingsException;
197 |
198 | /**
199 | * A settings builder interface.
200 | */
201 | interface Builder {
202 |
203 | /**
204 | * Builds the settings.
205 | */
206 | Settings build();
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/src/main/java/net/csdn/common/time/NumberExtendedForTime.java:
--------------------------------------------------------------------------------
1 | package net.csdn.common.time;
2 |
3 | import net.csdn.common.collections.WowCollections;
4 | import net.csdn.common.reflect.ReflectHelper;
5 | import org.joda.time.DateTime;
6 |
7 | import java.util.Collections;
8 | import java.util.Comparator;
9 | import java.util.List;
10 |
11 | /**
12 | * 4/14/13 WilliamZhu(allwefantasy@gmail.com)
13 | */
14 | public class NumberExtendedForTime {
15 | DateTime time = new DateTime();
16 | private long extened;
17 | private String type;
18 | private String operate;
19 |
20 | public NumberExtendedForTime(int int_extened) {
21 | this.extened = int_extened;
22 | }
23 |
24 | public NumberExtendedForTime(long int_extened) {
25 | this.extened = int_extened;
26 | }
27 |
28 | public NumberExtendedForTime day() {
29 | type = "Days";
30 | return this;
31 | }
32 |
33 | public NumberExtendedForTime minute() {
34 | type = "Minutes";
35 | return this;
36 | }
37 |
38 | public NumberExtendedForTime second() {
39 | type = "Seconds";
40 | return this;
41 | }
42 |
43 | public NumberExtendedForTime hour() {
44 | type = "Hours";
45 | return this;
46 | }
47 |
48 | public NumberExtendedForTime week() {
49 | type = "Weeks";
50 | return this;
51 | }
52 |
53 | public NumberExtendedForTime month() {
54 | type = "Months";
55 | return this;
56 | }
57 |
58 | public NumberExtendedForTime millis() {
59 | type = "Millis";
60 | return this;
61 | }
62 |
63 | public NumberExtendedForTime year() {
64 | type = "Years";
65 | return this;
66 | }
67 |
68 | public DateTime ago() {
69 | operate = "minus";
70 | return (DateTime) ReflectHelper.method(time, operate + type, extened);
71 | }
72 |
73 | public DateTime fromNow() {
74 | operate = "plus";
75 | return (DateTime) ReflectHelper.method(time, operate + type, extened);
76 | }
77 |
78 | public DateTime from_now() {
79 | return fromNow();
80 | }
81 |
82 | public static void main(String[] args) {
83 | List jack = WowCollections.list(3, 5, 1);
84 | Collections.sort(jack, new Comparator