Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding logging properties like FILE_LOG_PATTERN #1788

Closed
marceloverdijk opened this issue Oct 29, 2014 · 55 comments
Closed

Overriding logging properties like FILE_LOG_PATTERN #1788

marceloverdijk opened this issue Oct 29, 2014 · 55 comments
Assignees
Labels
type: enhancement A general enhancement
Milestone

Comments

@marceloverdijk
Copy link

Feature request for adding support for overriding logging properties via application.properties/yaml.

E.g. I want to easily override the FILE_LOG_PATTERN property.
To define this in the application.properties/yaml also gives the benefit that I can change it per environment.

Boot already offers

# LOGGING
logging.path=/var/logs
logging.file=myapp.log
logging.config= # location of config file (default classpath:logback.xml for logback)
logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF)

Something like below maybe?

logging.property.FILE_LOG_PATTERN=

Reference: http://stackoverflow.com/questions/26629197/overriding-the-file-log-pattern-if-possible-per-env

@dsyer dsyer added the type: enhancement A general enhancement label Oct 31, 2014
@backs999
Copy link

This would be very useful as I also have just come across the need to override the pattern for our logging indexer +1

@joensson
Copy link

+1 - This would indeed be very useful. We have the same need - and would also like to set other properties for use in logback config in the same manner.

@khoubyari
Copy link

+1

@philwebb
Copy link
Member

philwebb commented Dec 1, 2014

Seems like a popular request. I'll assign to 1.2 and take a look but we are really near GA so it might need to be pushed back to 1.3.

@philwebb philwebb added this to the 1.2.0 milestone Dec 1, 2014
@marceloverdijk
Copy link
Author

Since I raised this feature request I needed to log to 2 separate files. 1 standard log file (with a customized format containing MDC info) but also 1 log file to be used for Logstash/Elasticsearch with custom layout class.
Would it also be possible to configure multiple file appenders using these properties?

@philwebb
Copy link
Member

philwebb commented Dec 3, 2014

@marceloverdijk Sounds like a fairly complex scenario so it probably won't be possible with the properties. Have you tried defining your own logback.xml file? There are a couple imports that you can use (see this example).

@marceloverdijk
Copy link
Author

Yes I'm using a custom logback.xml now.
I only wished I could have a logback.xml per env.

Btw I think it should be possible to define multiple appenders via properties. With standard log4j properties file this is also possible. Thinking of it I wonder if it would be possible to define log4j.* properties (in applucation properties) and use them to initiate log4j? Just thinking loud to see options for customizations and per env config.

@joensson
Copy link

joensson commented Dec 4, 2014

Have not tried it myself - but for environment specific logback configurations I think it is possible to do that using logback includes.

Create a 'master' logback configuration file that includes another environment specific logback-staging.xml - that has root tag 'included'.

logback-master.xml:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="logback-${LOGBACK_ENV}.xml"/>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
...
<!-- do something with the MY_PROPERTY value -->
...
</configuration>

logback-test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<included>
    <property name="MY_PROPERTY" value="test"/>
...
</included>

When launching your application set the LOGBACK_ENV system property, e.g. System.setProperty("LOGBACK_ENV", "test") (this will of course not be necessary when the current issue has been fixed - at that time we can simple set a property in application.properties instead) and if the file exists you should see a log line from logback:
INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at ...

  • almost lige we did in Spring in the good old pre-profile days when wanting different beans/properties per environment.

@philwebb
Copy link
Member

philwebb commented Dec 9, 2014

I'm afraid I need to push this back for now :(

@philwebb philwebb modified the milestones: 1.3.0, 1.2.0 Dec 9, 2014
@marceloverdijk
Copy link
Author

Hopefully another day maybe :-)

On 09 Dec 2014, at 19:31, Phil Webb notifications@github.com wrote:

I'm afraid I need to push this back for now :(


Reply to this email directly or view it on GitHub.

@ljramones
Copy link

+1
agreed, this is a good feature.

@ghost
Copy link

ghost commented Feb 28, 2015

+1
nice to have.

@brenuart
Copy link
Contributor

brenuart commented Mar 5, 2015

+1
nice and avoids having to duplicate all of the standard logback.xml configuration files provided with SpringBoot.

@cmoro-deusto
Copy link

+1 thanks very much.

@txiong1987
Copy link

+1 Thanks!

@edudar
Copy link

edudar commented Mar 31, 2015

+1
Can you go beyond just FILE_LOG_PATTERN here? I suspect this duplicates what @marceloverdijk asked above to some extend. We have custom Logback appender that talks to fluentd. Nothing fancy, new xml that include base one and has its own appender. There are 3 properties though: tag, host and port. We can configure them now through -D option but it would be nice to have streamlined way to define logging properties that will be replaced in the same way LOG_FILE with logging.file work.

@igler
Copy link

igler commented May 4, 2015

+1

1 similar comment
@cannibalcow
Copy link

+1

@nagase2
Copy link

nagase2 commented May 17, 2015

I also struggle with same issue because the log message issued twice.
Eventually, I stop including base.xml at this
moment...
+1

@Chumper
Copy link

Chumper commented May 26, 2015

👍 the same here, i want to use the AmqpAppender from spring-amqp

The problem is, that i need to get the host, username, password and port from the application config.

@iamiddy
Copy link

iamiddy commented Jun 1, 2015

+1 need this feature,
opened this SO before stumbled here

@hmeerlo
Copy link

hmeerlo commented Jun 4, 2015

+1

@cbelleza
Copy link

cbelleza commented Jun 6, 2015

Missing it a lot... When will it be done?

@snicoll snicoll self-assigned this Jun 8, 2015
@shaqtsui
Copy link

shaqtsui commented Jun 9, 2015

+1

@spencergibb
Copy link
Member

+1 for spring-cloud-sleuth-correlation including a correlation id in the logs.

lukashinsch added a commit to lukashinsch/spring-boot that referenced this issue Jun 26, 2015
Makes spring properties starting with prefix "logging.properties."
available as system properties (without the prefix) before the logging
system is initialized so they can be used in logging configurations
that support them (e.g. logback).

Closes spring-projectsgh-1788
philwebb added a commit that referenced this issue Jun 26, 2015
Update logging documentation to show the new `-spring` convention and
the Logback extensions.

See gh-1788
See gh-2558
See gh-3338
@philwebb
Copy link
Member

For anyone watching this issue there is now a new <springProperty> tag that you can use to pull in Environment properties. To use it you first need to rename your logback.xml file to logback-spring.xml. See the updated documentation for details. There's also a new <springProfile> tag that can be used to optionally include/exclude configuration.

I'd appreciate any feedback on this approach, does it work for people?

@ccit-spence
Copy link

How does this differ from using Spring Cloud Config? A couple of months ago I needed the functionality for determining which profile was loaded and was able to accomplish with Spring Cloud Config. Essentially loading profile specific logback configurations. Is this the same concept?

@philwebb
Copy link
Member

@ccit-spence can you point me at a sample to show me what you mean?

@ccit-spence
Copy link

@philwebb I didn't have to really add anything except for configuring the service to use Spring Cloud Config It was really the only thing I have to do. In each service I have the following logback profile specific files. I do not have a logback.xml file. Only with its respective profile as below.

logback-local.xml
logback-docker.xml
logback-production.xml

Your example seems to add more robust capabilities. I am pretty sure the reason why Spring Cloud Config worked is that it refreshes the logback config after the initial load allowing for later processing during boot.

@philwebb
Copy link
Member

@spencergibb @dsyer How does Spring Cloud Config support these profile specific logback configs? Is this something we should bring into Spring Boot? How does it deal with multiple active profiles?

@dsyer
Copy link
Member

dsyer commented Jun 29, 2015

IIRC there is some hackery in Spring Cloud Commons to explicitly give log configuration a chance to refresh before starting to main application context. Remember that Spring Cloud always has a parent "bootstrap" context and it is there that you can set up things that need extra logic before the main context has started. I'm not sure there is anything magic about it in general, but logging systems require special treatment to get them initialized early, so I think that led to a special case.

@rob-baily
Copy link

@philwebb I like the approach as it seems very flexible. Based on this is it possible to update properties that are defined in defaults.xml like the CONSOLE_LOG_PATTERN and FILE_LOG_PATTERN from another property source? It seems like it would be useful to override just these and not completely define a new configuration just to override this.

@philwebb
Copy link
Member

@rob-baily I've raised #3367 to consider this. We can't use the same <springProperty/> approach since base.xml might be imported from from a standard logback config where we don't have the extensions.

@rakesh9
Copy link

rakesh9 commented Jul 1, 2015

I am trying to use springProfile tag in logback.xml to have profile specific logging but spring boot is ignoring this and all tags after this.(I am using spring boot version 1.2.4 Release)
-ERROR in ch.qos.logback.core.joran.spi.Interpreter@5:47 - no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]

-ERROR in ch.qos.logback.core.joran.spi.Interpreter@6:88 - no applicable action for [appender], current ElementPath is [[configuration][springProfile][appender]]

I tried putting the same configuration in logback-spring.xml but spring boot not recognizing and loading this file itself.
Please guide me how to use springProfile tag to get profile specific logging.

@rob-baily
Copy link

@rakesh9 The new feature is coming is version 1.3.0. You can't use it until this unless you want to go against a SNAPSHOT build.

@philwebb
Copy link
Member

philwebb commented Jul 1, 2015

@rakesh9 as @rob-baily said this is a new feature not and won't be available in 1.2.x. You'll also need to use logback-spring.xml as your filename when you do migrate (see the updated docs).

@rakesh9
Copy link

rakesh9 commented Jul 2, 2015

@rob-baily @ philwebb Thanks for the update..I missed observing the version...I am new to spring-boot..
can you suggest me any best alternative to have conditional logging for diff environments like dev,test using logback.groovy in 1.2.4 version

@tednology
Copy link

@philwebb Love the the new <springProfile> element in 1.3.0.M2, though I had a question:

The name attribute of <springProfile> doesn't seem to accept a list of profiles (I've tried with spaces, commas, semi-colons), is this working as intended?

@philwebb
Copy link
Member

@edward-smith It only accepts a single name at the moment, but it should be easy to fix that. I'll open a new issue.

@thefalconfeat
Copy link

+1

@snicoll
Copy link
Member

snicoll commented Jul 28, 2015

@thefalconfeat it's already implemented (and this issue is closed FWIW).

@lhanson
Copy link

lhanson commented Apr 27, 2016

Is there any support for this functionality in logback-spring.groovy, or is this XML-only?

@wilkinsona
Copy link
Member

@lhanson It's XML only. We're not sure that it's possible with Logback's Groovy support. See #5051 for details.

@lhanson
Copy link

lhanson commented Apr 27, 2016

@wilkinsona Great, thanks. I think I'll be able to express my setup in XML, so this feature is still a big help.

@Tetradeus
Copy link

Any chance to see this feature (springproperty) extended for log4j2 ?

@wilkinsona
Copy link
Member

@Tetradeus We're not sure that it's possible with Log4j2. If it is, the change would be made as part of #4032.

@mageddo
Copy link

mageddo commented Mar 7, 2017

I want to know where Spring specify that spring boot 1.2 not support log pattern set by application.properties, man, I take a while to get it.

@wilkinsona
Copy link
Member

The string FILE_LOG_PATTERN doesn't appear anywhere in the documentation for 1.2, whereas it appears in the documentation for 1.3 and onwards. I'm not sure that there's much more that we can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.