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.ThroughputLogger; 026import org.apache.camel.spi.ExchangeFormatter; 027import org.apache.camel.spi.Metadata; 028import org.apache.camel.spi.UriEndpoint; 029import org.apache.camel.spi.UriParam; 030import org.apache.camel.spi.UriPath; 031import org.apache.camel.util.CamelLogger; 032import org.apache.camel.util.ServiceHelper; 033import org.slf4j.Logger; 034 035/** 036 * Logger endpoint. 037 */ 038@UriEndpoint(scheme = "log", title = "Log", syntax = "log:loggerName", producerOnly = true, label = "core,monitoring") 039public class LogEndpoint extends ProcessorEndpoint { 040 041 private volatile Processor logger; 042 private Logger providedLogger; 043 private ExchangeFormatter localFormatter; 044 @UriPath(description = "Name of the logging category to use") @Metadata(required = "true") 045 private String loggerName; 046 @UriParam(defaultValue = "INFO", enums = "ERROR,WARN,INFO,DEBUG,TRACE,OFF") 047 private String level; 048 @UriParam 049 private String marker; 050 @UriParam 051 private Integer groupSize; 052 @UriParam 053 private Long groupInterval; 054 @UriParam(defaultValue = "true") 055 private Boolean groupActiveOnly; 056 @UriParam 057 private Long groupDelay; 058 059 public LogEndpoint() { 060 } 061 062 public LogEndpoint(String endpointUri, Component component) { 063 super(endpointUri, component); 064 } 065 066 public LogEndpoint(String endpointUri, Component component, Processor logger) { 067 super(endpointUri, component); 068 setLogger(logger); 069 } 070 071 @Override 072 protected void doStart() throws Exception { 073 if (logger == null) { 074 // setup a new logger here 075 CamelLogger camelLogger; 076 LoggingLevel loggingLevel = LoggingLevel.INFO; 077 if (level != null) { 078 loggingLevel = LoggingLevel.valueOf(level); 079 } 080 if (providedLogger == null) { 081 camelLogger = new CamelLogger(loggerName, loggingLevel, getMarker()); 082 } else { 083 camelLogger = new CamelLogger(providedLogger, loggingLevel, getMarker()); 084 } 085 if (getGroupSize() != null) { 086 logger = new ThroughputLogger(camelLogger, getGroupSize()); 087 } else if (getGroupInterval() != null) { 088 Boolean groupActiveOnly = getGroupActiveOnly() != null ? getGroupActiveOnly() : Boolean.TRUE; 089 Long groupDelay = getGroupDelay(); 090 logger = new ThroughputLogger(camelLogger, this.getCamelContext(), getGroupInterval(), groupDelay, groupActiveOnly); 091 } else { 092 logger = new CamelLogProcessor(camelLogger, localFormatter); 093 } 094 // the logger is the processor 095 setProcessor(this.logger); 096 097 } 098 ServiceHelper.startService(logger); 099 } 100 101 @Override 102 protected void doStop() throws Exception { 103 ServiceHelper.stopService(logger); 104 } 105 106 public void setLogger(Processor logger) { 107 this.logger = logger; 108 // the logger is the processor 109 setProcessor(this.logger); 110 } 111 112 public Processor getLogger() { 113 return logger; 114 } 115 116 @Override 117 public Producer createProducer() throws Exception { 118 return new LogProducer(this, this.logger); 119 } 120 121 @Override 122 protected String createEndpointUri() { 123 return "log:" + logger.toString(); 124 } 125 126 /** 127 * Logging level to use. 128 * <p/> 129 * The default value is INFO. 130 */ 131 public String getLevel() { 132 return level; 133 } 134 135 /** 136 * Logging level to use. 137 * <p/> 138 * The default value is INFO. 139 */ 140 public void setLevel(String level) { 141 this.level = level; 142 } 143 144 /** 145 * An optional Marker name to use. 146 */ 147 public String getMarker() { 148 return marker; 149 } 150 151 /** 152 * An optional Marker name to use. 153 */ 154 public void setMarker(String marker) { 155 this.marker = marker; 156 } 157 158 /** 159 * An integer that specifies a group size for throughput logging. 160 */ 161 public Integer getGroupSize() { 162 return groupSize; 163 } 164 165 /** 166 * An integer that specifies a group size for throughput logging. 167 */ 168 public void setGroupSize(Integer groupSize) { 169 this.groupSize = groupSize; 170 } 171 172 /** 173 * If specified will group message stats by this time interval (in millis) 174 */ 175 public Long getGroupInterval() { 176 return groupInterval; 177 } 178 179 /** 180 * If specified will group message stats by this time interval (in millis) 181 */ 182 public void setGroupInterval(Long groupInterval) { 183 this.groupInterval = groupInterval; 184 } 185 186 /** 187 * If true, will hide stats when no new messages have been received for a time interval, if false, show stats regardless of message traffic. 188 */ 189 public Boolean getGroupActiveOnly() { 190 return groupActiveOnly; 191 } 192 193 /** 194 * If true, will hide stats when no new messages have been received for a time interval, if false, show stats regardless of message traffic. 195 */ 196 public void setGroupActiveOnly(Boolean groupActiveOnly) { 197 this.groupActiveOnly = groupActiveOnly; 198 } 199 200 /** 201 * Set the initial delay for stats (in millis) 202 */ 203 public Long getGroupDelay() { 204 return groupDelay; 205 } 206 207 /** 208 * Set the initial delay for stats (in millis) 209 */ 210 public void setGroupDelay(Long groupDelay) { 211 this.groupDelay = groupDelay; 212 } 213 214 public ExchangeFormatter getLocalFormatter() { 215 return localFormatter; 216 } 217 218 public void setLocalFormatter(ExchangeFormatter localFormatter) { 219 this.localFormatter = localFormatter; 220 } 221 222 public Logger getProvidedLogger() { 223 return providedLogger; 224 } 225 226 public void setProvidedLogger(Logger providedLogger) { 227 this.providedLogger = providedLogger; 228 } 229 230 /** 231 * The logger name to use 232 */ 233 public String getLoggerName() { 234 return loggerName; 235 } 236 237 /** 238 * The logger name to use 239 */ 240 public void setLoggerName(String loggerName) { 241 this.loggerName = loggerName; 242 } 243}