001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.component.log; 018 019import org.apache.camel.Component; 020import org.apache.camel.LoggingLevel; 021import org.apache.camel.Processor; 022import org.apache.camel.Producer; 023import org.apache.camel.impl.ProcessorEndpoint; 024import org.apache.camel.processor.CamelLogProcessor; 025import org.apache.camel.processor.DefaultExchangeFormatter; 026import org.apache.camel.processor.ThroughputLogger; 027import org.apache.camel.spi.ExchangeFormatter; 028import org.apache.camel.spi.Metadata; 029import org.apache.camel.spi.UriEndpoint; 030import org.apache.camel.spi.UriParam; 031import org.apache.camel.spi.UriParams; 032import org.apache.camel.spi.UriPath; 033import org.apache.camel.util.CamelLogger; 034import org.apache.camel.util.ServiceHelper; 035import org.slf4j.Logger; 036 037/** 038 * Logger endpoint. 039 */ 040@UriEndpoint(scheme = "log", title = "Log", syntax = "log:loggerName", producerOnly = true, label = "core,monitoring") 041public class LogEndpoint extends ProcessorEndpoint { 042 043 private volatile Processor logger; 044 private Logger providedLogger; 045 private ExchangeFormatter localFormatter; 046 047 @UriPath(description = "Name of the logging category to use") @Metadata(required = "true") 048 private String loggerName; 049 @UriParam(defaultValue = "INFO", enums = "ERROR,WARN,INFO,DEBUG,TRACE,OFF") 050 private String level; 051 @UriParam 052 private String marker; 053 @UriParam 054 private Integer groupSize; 055 @UriParam 056 private Long groupInterval; 057 @UriParam(defaultValue = "true") 058 private Boolean groupActiveOnly; 059 @UriParam 060 private Long groupDelay; 061 // we want to include the uri options of the DefaultExchangeFormatter 062 @UriParam 063 private DefaultExchangeFormatter exchangeFormatter; 064 065 public LogEndpoint() { 066 } 067 068 public LogEndpoint(String endpointUri, Component component) { 069 super(endpointUri, component); 070 } 071 072 public LogEndpoint(String endpointUri, Component component, Processor logger) { 073 super(endpointUri, component); 074 setLogger(logger); 075 } 076 077 @Override 078 protected void doStart() throws Exception { 079 if (logger == null) { 080 // setup a new logger here 081 CamelLogger camelLogger; 082 LoggingLevel loggingLevel = LoggingLevel.INFO; 083 if (level != null) { 084 loggingLevel = LoggingLevel.valueOf(level); 085 } 086 if (providedLogger == null) { 087 camelLogger = new CamelLogger(loggerName, loggingLevel, getMarker()); 088 } else { 089 camelLogger = new CamelLogger(providedLogger, loggingLevel, getMarker()); 090 } 091 if (getGroupSize() != null) { 092 logger = new ThroughputLogger(camelLogger, getGroupSize()); 093 } else if (getGroupInterval() != null) { 094 Boolean groupActiveOnly = getGroupActiveOnly() != null ? getGroupActiveOnly() : Boolean.TRUE; 095 Long groupDelay = getGroupDelay(); 096 logger = new ThroughputLogger(camelLogger, this.getCamelContext(), getGroupInterval(), groupDelay, groupActiveOnly); 097 } else { 098 logger = new CamelLogProcessor(camelLogger, localFormatter); 099 } 100 // the logger is the processor 101 setProcessor(this.logger); 102 103 } 104 ServiceHelper.startService(logger); 105 } 106 107 @Override 108 protected void doStop() throws Exception { 109 ServiceHelper.stopService(logger); 110 } 111 112 public void setLogger(Processor logger) { 113 this.logger = logger; 114 // the logger is the processor 115 setProcessor(this.logger); 116 } 117 118 public Processor getLogger() { 119 return logger; 120 } 121 122 @Override 123 public Producer createProducer() throws Exception { 124 return new LogProducer(this, this.logger); 125 } 126 127 @Override 128 protected String createEndpointUri() { 129 return "log:" + logger.toString(); 130 } 131 132 /** 133 * Logging level to use. 134 * <p/> 135 * The default value is INFO. 136 */ 137 public String getLevel() { 138 return level; 139 } 140 141 /** 142 * Logging level to use. 143 * <p/> 144 * The default value is INFO. 145 */ 146 public void setLevel(String level) { 147 this.level = level; 148 } 149 150 /** 151 * An optional Marker name to use. 152 */ 153 public String getMarker() { 154 return marker; 155 } 156 157 /** 158 * An optional Marker name to use. 159 */ 160 public void setMarker(String marker) { 161 this.marker = marker; 162 } 163 164 /** 165 * An integer that specifies a group size for throughput logging. 166 */ 167 public Integer getGroupSize() { 168 return groupSize; 169 } 170 171 /** 172 * An integer that specifies a group size for throughput logging. 173 */ 174 public void setGroupSize(Integer groupSize) { 175 this.groupSize = groupSize; 176 } 177 178 /** 179 * If specified will group message stats by this time interval (in millis) 180 */ 181 public Long getGroupInterval() { 182 return groupInterval; 183 } 184 185 /** 186 * If specified will group message stats by this time interval (in millis) 187 */ 188 public void setGroupInterval(Long groupInterval) { 189 this.groupInterval = groupInterval; 190 } 191 192 /** 193 * If true, will hide stats when no new messages have been received for a time interval, if false, show stats regardless of message traffic. 194 */ 195 public Boolean getGroupActiveOnly() { 196 return groupActiveOnly; 197 } 198 199 /** 200 * If true, will hide stats when no new messages have been received for a time interval, if false, show stats regardless of message traffic. 201 */ 202 public void setGroupActiveOnly(Boolean groupActiveOnly) { 203 this.groupActiveOnly = groupActiveOnly; 204 } 205 206 /** 207 * Set the initial delay for stats (in millis) 208 */ 209 public Long getGroupDelay() { 210 return groupDelay; 211 } 212 213 /** 214 * Set the initial delay for stats (in millis) 215 */ 216 public void setGroupDelay(Long groupDelay) { 217 this.groupDelay = groupDelay; 218 } 219 220 public ExchangeFormatter getLocalFormatter() { 221 return localFormatter; 222 } 223 224 public void setLocalFormatter(ExchangeFormatter localFormatter) { 225 this.localFormatter = localFormatter; 226 } 227 228 public Logger getProvidedLogger() { 229 return providedLogger; 230 } 231 232 public void setProvidedLogger(Logger providedLogger) { 233 this.providedLogger = providedLogger; 234 } 235 236 /** 237 * The logger name to use 238 */ 239 public String getLoggerName() { 240 return loggerName; 241 } 242 243 /** 244 * The logger name to use 245 */ 246 public void setLoggerName(String loggerName) { 247 this.loggerName = loggerName; 248 } 249}