[logback-user] Logback-beagle with config file used for multiple environments
ceki
ceki at qos.ch
Fri Jul 27 08:36:44 CEST 2012
On 25.07.2012 00:31, Noremac wrote:
> I found the newly released Logback-Beagle to look pretty cool. I want it!
>
> However, I see that you need to add the tag <consolePlugin /> to the config
> file. This makes me worry a little about the implications on this when that
> same config file is used in test and production.
>
> So it looks like if I add the consolePlugin then I'll be having an added
> appender trying to spit out logs to the local machine (not desired).
>
> Basically, I only want the extra appender in development. Is there any other
> way to do this other than doing it programmatically (by doing a check on
> which environment it is in).
You have several options:
1) use logback-text.xml for eh... testing.
2a) conditional logging. See also the documentation at [1].
Assuming "hulk" is the name of the production machine, you could enable
sending events to the console plugin on machines other than hulk as follows:
<configuration>
<if condition='!property("HOSTNAME").contains("hulk")'/>
<consolePlugin/>
</if>
..... other configuration directives
</configuration>
Note that conditional configuration requires Janino.
2b) use some preset system variable, say "deploymentType":
<configuration>
<if condition='property("deploymentType").equals("development")'>
<consolePlugin/>
</if>
..... other configuration directives
</configuration>
2c) compute the deployment type dynamically. See the docs at [2].
<configuration>
<define name="deploymentType"
class="a.class.implementing.PropertyDefiner">
</define>
<if condition='property("deploymentType").equals("development")'>
<consolePlugin/>
</if>
..... other configuration directives
</configuration>
3) Programmatically enable sending events to the console plugin. Here is
the code:
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.net.SocketAppender;
import ch.qos.logback.classic.Logger;
import static org.slf4j.Logger.ROOT_LOGGER_NAME;
....
String hostname = "localhost";
int port = 4321;
SocketAppender socketAppender = new SocketAppender();
socketAppender.setRemoteHost(hostname);
socketAppender.setPort(port);
socketAppender.setIncludeCallerData(true);
socketAppender.setReconnectionDelay(10000);
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
socketAppender.setContext(lc);
socketAppender.start();
// LoggerFactory return type is org.slf4j.Logger
Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);
rootLogger.addAppender(socketAppender);
HTH,
[1] http://logback.qos.ch/manual/configuration.html#conditional
[2] http://logback.qos.ch/manual/configuration.html#definingPropsOnTheFly
--
Ceki
http://tinyurl.com/proLogback
More information about the Logback-user
mailing list