129

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?

Community
  • 1
  • 1
Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
  • 40
    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 ` `. Can't stand warnings on my projects, OCD I guess. – Lucas Dec 27 '12 at 19:37
  • 6
    @Lucas Not so OCD I'd say. Irrelevant warnings can shadow important warnings. Just happened to me. – raphinesse Jul 12 '14 at 15:16
  • 2
    I'm a bit compulsive like that, too, but adding this line as you suggest did nothing to remove the warning for me... any idea?? – Marc Oct 28 '14 at 15:48
  • 1
    After adding the line, one has to trigger validation e.g. via context-menu 'Validate'. – Manuel Jan 09 '17 at 09:31

7 Answers7

86

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.

Adrian Ber
  • 20,474
  • 12
  • 67
  • 117
85

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.

Tuna
  • 2,937
  • 4
  • 37
  • 61
Eugene Yokota
  • 94,654
  • 45
  • 215
  • 319
  • 30
    In the same way that English language is extremely flexible therefore it has no grammatical rules???? What a cop-out of an excuse! – Ken Alton Mar 07 '17 at 14:21
36

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.

Sergey
  • 3,253
  • 2
  • 33
  • 55
  • 1
    This solution works. Just put `logback.xsd` in the same directory as `logback.xml` and add it to your project manually. In IntelliJ, for example, you'd go to `Settings > Languages & Frameworks > Schemas and DTDs` and click the green "+" to add an external resource. Set "URI" to `http://ch.qos.logback/xml/ns/logback` and "Location" to the location of `logback.xsd`. – Jonathan E. Landrum Apr 05 '17 at 15:13
  • 1
    I just created an url shortener for this project's logback.xsd file, and it's working as expected. You can use `xsi:schemaLocation="http://ch.qos.logback/xml/ns/logback https://git.io/logback.xsd"` – Osguima3 Jan 18 '18 at 09:20
3

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.

nkatsar
  • 1,580
  • 17
  • 15
  • 1
    It has now been merged. According to [Github Network Graph](https://github.com/enricopulatzo/logback-XSD/network), [enricopulatzo](https://github.com/enricopulatzo/logback-XSD)’s schema seems to be the richest source. – Olivier Cailloux Aug 22 '16 at 21:02
2
<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

  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Rohit Gupta Jul 08 '15 at 00:52
  • 1
    How complete support is there in that repo? – Vladislav Rastrusny Jul 08 '15 at 14:40
  • 1
    For those coming years after like me, the previous xsd location is not valid anymore. You can refer to https://git.io/logback.xsd as mentioned in a previous response – gavard.e Nov 20 '19 at 08:55
  • The solution from this answer **does NOT work** anymore. – rustyx Apr 02 '22 at 21:08
1

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

This will allow eclipse to autocomplete and validate if xml is not using plugins or other extensions mechanisms:

<?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
">
raisercostin
  • 8,777
  • 5
  • 67
  • 76
  • even using this, I still get validation errors on unrecognized XML elements... like `` under an ` – maxxyme Nov 05 '20 at 23:37
  • I doubt that a perfect xsd will exist since logback config supports plugins and each of them is free to have their own tags. – raisercostin Nov 06 '20 at 07:18
  • even when the plugins are the ones from Logback itself? or I speak about appenders, I don't know what plugins for Logback are about. but not having a "perfect xsd" defeats the original concept of XML, i.e. having a Markup Language, which by the way is eXtensible (e.g. through additional definitions/schemas - the Spring Framework handles this perfectly well when you configure it in XML). – maxxyme Nov 08 '20 at 14:35
  • While you are right on xml, it is known that it has practical limits. With infinite time and effort you can make xml schema work but in the real world is not the case. I cannot say more than the official documentation: "As will be demonstrated over and over, the syntax of logback configuration files is extremely flexible. As such, it is not possible to specify the allowed syntax with a DTD file or an XML schema." - http://logback.qos.ch/manual/configuration.html#syntax – raisercostin Nov 09 '20 at 18:03
0

<?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
">

use this lines . it download automatically from remote. we need not to downlaod the file

Prabu
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 23 '23 at 04:59