|
| 1 | + |
| 2 | + |
| 3 | +// -----( IS Java Code Template v1.2 |
| 4 | + |
| 5 | +import com.wm.data.*; |
| 6 | +import com.wm.util.Values; |
| 7 | +import com.wm.app.b2b.server.Service; |
| 8 | +import com.wm.app.b2b.server.ServiceException; |
| 9 | +// --- <<IS-START-IMPORTS>> --- |
| 10 | +import java.io.File; |
| 11 | +import java.io.FileInputStream; |
| 12 | +import java.io.FileNotFoundException; |
| 13 | +import java.io.IOException; |
| 14 | +import org.apache.logging.log4j.Level; |
| 15 | +import org.apache.logging.log4j.Logger; |
| 16 | +import org.apache.logging.log4j.core.LoggerContext; |
| 17 | +import org.apache.logging.log4j.core.config.Configuration; |
| 18 | +import org.apache.logging.log4j.core.config.ConfigurationFactory; |
| 19 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
| 20 | +import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory; |
| 21 | +// --- <<IS-END-IMPORTS>> --- |
| 22 | + |
| 23 | +public final class Log4jDemo |
| 24 | + |
| 25 | +{ |
| 26 | + // ---( internal utility methods )--- |
| 27 | + |
| 28 | + final static Log4jDemo _instance = new Log4jDemo(); |
| 29 | + |
| 30 | + static Log4jDemo _newInstance() { return new Log4jDemo(); } |
| 31 | + |
| 32 | + static Log4jDemo _cast(Object o) { return (Log4jDemo)o; } |
| 33 | + |
| 34 | + // ---( server methods )--- |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + public static final void init (IData pipeline) |
| 40 | + throws ServiceException |
| 41 | + { |
| 42 | + // --- <<IS-START(init)>> --- |
| 43 | + // @sigtype java 3.5 |
| 44 | + File log4jConfigFile = new File(LOG4J_CFG); |
| 45 | + |
| 46 | + if (log4jConfigFile.exists() && log4jConfigFile.canRead()) { |
| 47 | + |
| 48 | + ConfigurationFactory factory = XmlConfigurationFactory.getInstance(); |
| 49 | + ConfigurationSource configurationSource = null; |
| 50 | + try { |
| 51 | + configurationSource = new ConfigurationSource(new FileInputStream(log4jConfigFile)); |
| 52 | + } catch (FileNotFoundException e) { |
| 53 | + // TODO Auto-generated catch block |
| 54 | + e.printStackTrace(); |
| 55 | + } catch (IOException e) { |
| 56 | + // TODO Auto-generated catch block |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + Configuration configuration = factory.getConfiguration(logCtx, configurationSource); |
| 60 | + |
| 61 | + // Get context instance |
| 62 | + logCtx = new LoggerContext(PACKAGE_NAME); |
| 63 | + |
| 64 | + // Start context |
| 65 | + logCtx.start(configuration); |
| 66 | + } else { |
| 67 | + throw new ServiceException("Configuration file '" + LOG4J_CFG + "' does not exist or cannot be read"); |
| 68 | + } |
| 69 | + // --- <<IS-END>> --- |
| 70 | + |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + public static final void log (IData pipeline) |
| 77 | + throws ServiceException |
| 78 | + { |
| 79 | + // --- <<IS-START(log)>> --- |
| 80 | + // @sigtype java 3.5 |
| 81 | + // [i] field:0:required message |
| 82 | + // [i] field:0:required level {"TRACE","DEBUG","INFO","WARN","ERROR","FATAL"} |
| 83 | + // [i] field:0:required logger |
| 84 | + |
| 85 | + // pipeline |
| 86 | + IDataCursor pipelineCursor = pipeline.getCursor(); |
| 87 | + try { |
| 88 | + String message = IDataUtil.getString( pipelineCursor, "message" ); |
| 89 | + String levelStr = IDataUtil.getString( pipelineCursor, "level" ); |
| 90 | + String loggerStr = IDataUtil.getString( pipelineCursor, "logger" ); |
| 91 | + |
| 92 | + Level level = Level.getLevel(levelStr); |
| 93 | + Logger logger = logCtx.getLogger(loggerStr); |
| 94 | + |
| 95 | + if (logger.isEnabled(level)) { |
| 96 | + logger.log(level, message); |
| 97 | + } |
| 98 | + |
| 99 | + } finally { |
| 100 | + pipelineCursor.destroy(); |
| 101 | + } |
| 102 | + |
| 103 | + // pipeline |
| 104 | + // --- <<IS-END>> --- |
| 105 | + |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + public static final void shutdown (IData pipeline) |
| 112 | + throws ServiceException |
| 113 | + { |
| 114 | + // --- <<IS-START(shutdown)>> --- |
| 115 | + // @sigtype java 3.5 |
| 116 | + if (logCtx != null) { |
| 117 | + logCtx.stop(); |
| 118 | + } else { |
| 119 | + throw new ServiceException("Logger '" + PACKAGE_NAME + "' not initialized"); |
| 120 | + } |
| 121 | + // --- <<IS-END>> --- |
| 122 | + |
| 123 | + |
| 124 | + } |
| 125 | + |
| 126 | + // --- <<IS-START-SHARED>> --- |
| 127 | + |
| 128 | + private static final String PACKAGE_NAME = "Log4jDemo"; |
| 129 | + private static final String LOG4J_CFG = "./packages/" + PACKAGE_NAME + "/config/log4j2.xml"; |
| 130 | + private static LoggerContext logCtx = null; |
| 131 | + |
| 132 | + // --- <<IS-END-SHARED>> --- |
| 133 | +} |
| 134 | + |
0 commit comments