116
votes

I've seen several discussions on the net about how great it would be to have an XML schema or DTD for logback.xml file to have at least the very basic validation and auto-completion in IDEs like IDEA or Eclipse, but I never saw any solution.

Did you?

6
As far as validation and auto-complete, see answers below... but to get eclipse to stop warning me, I use a trusty old DTD of <!DOCTYPE configuration>. Can't stand warnings on my projects, OCD I guess.Lucas
@Lucas Not so OCD I'd say. Irrelevant warnings can shadow important warnings. Just happened to me.raphinesse
I'm a bit compulsive like that, too, but adding this <!DOCTYPE configuration> line as you suggest did nothing to remove the warning for me... any idea??Marc
After adding the <!DOCTYPE configuration> line, one has to trigger validation e.g. via context-menu 'Validate'.Manuel

6 Answers

34
votes

It is not supported officially according to the documentation, but there is an independent project to provide Schema for Logback

However, due to extreme flexibility of the Logback configuration, Schema cannot support all possible configuration options.

83
votes

As of June 2011, the official documentation states

As shall become clear, the syntax of logback configuration files is extremely flexible. As such, it is not possible specify the allowed syntax with a DTD file or an XML Schema.

There was a brief thread on the topic, but didn't seem to go anywhere.

81
votes

Just to get rid of the annoying warning in Eclipse add <!DOCTYPE xml> after <?xml version="1.0" encoding="UTF-8"?>.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>

To get rid of the exclamation point icon after you fix it, you might need to trigger the validation again by right-clicking the file and choosing Validate.

3
votes

The independent project mentioned by Sergey covered most of my requirements.

However, some elements were missing, I added them on my own fork on on https://github.com/nkatsar/logback-XSD. Hope they will get merged in the main project.

2
votes
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.padual.com/java/logback.xsd">

from GitHub "An XML Schema Definition for logback" https://github.com/nkatsar/logback-XSD

-1
votes

A slightly different answer that allows eclipse autocomplete is:

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.padual.com/java/logback.xsd" 
    debug="true" packagingData="true" scan="true" scanPeriod="30 seconds">

Update 2020-11-02

Thanks to https://github.com/enricopulatzo/logback-XSD

<?xml version="1.0" encoding="UTF-8"?>
<configuration
    xmlns="http://ch.qos.logback/xml/ns/logback"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://raw.githubusercontent.com/enricopulatzo/logback-XSD/master/src/main/xsd/logback.xsd">