-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog4j-to-logback.xsl
154 lines (138 loc) · 6.44 KB
/
log4j-to-logback.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!--
This XLST script converts log4j.xml file to logback.xml file
trying to mimic its behavior as close as possible
@author Roman Puchkovskiy
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="log4j xalan"
xmlns:log4j="http://jakarta.apache.org/log4j/"
xmlns:xalan="http://xml.apache.org/xslt"
xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" xalan:indent-amount="4"/>
<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:template match="/log4j:configuration">
<configuration scan="true" scanPeriod="10 seconds">
<xsl:apply-templates select="appender"/>
<xsl:apply-templates select="logger"/>
<xsl:apply-templates select="root"/>
<xsl:apply-templates select="comment()"/>
</configuration>
</xsl:template>
<xsl:template match="appender">
<appender>
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="class">
<xsl:choose>
<xsl:when test="@class = 'org.apache.log4j.ConsoleAppender'">ch.qos.logback.core.ConsoleAppender</xsl:when>
<xsl:when test="@class = 'org.apache.log4j.net.SMTPAppender'">ch.qos.logback.classic.net.SMTPAppender</xsl:when>
<xsl:when test="@class = 'org.apache.log4j.net.SocketAppender'">ch.qos.logback.classic.net.SocketAppender</xsl:when>
<xsl:when test="@class = 'org.apache.log4j.net.SyslogAppender'">ch.qos.logback.classic.net.SyslogAppender</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">Unknown appender class: <xsl:value-of select="@class"/></xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates select="param"/>
<xsl:apply-templates select="layout"/>
<xsl:apply-templates select="filter"/>
</appender>
<xsl:call-template name="newline"/>
</xsl:template>
<xsl:template match="param">
<xsl:choose>
<xsl:when test="@name = 'SMTPHost'">
<smtpHost><xsl:value-of select="@value"/></smtpHost>
</xsl:when>
<xsl:when test="@name = 'BufferSize'">
<!-- ignoring this parameter -->
</xsl:when>
<xsl:otherwise>
<!-- lowercasing the first character -->
<xsl:element name="{concat(translate(substring(@name,1,1), $vUpper, $vLower),
substring(@name, 2)
)
}">
<xsl:value-of select="@value"/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="layout">
<xsl:choose>
<xsl:when test="@class = 'org.apache.log4j.PatternLayout'">
<xsl:choose>
<xsl:when test="../@class = 'org.apache.log4j.ConsoleAppender'">
<encoder>
<pattern><xsl:value-of select="param[@name = 'ConversionPattern']/@value"/></pattern>
</encoder>
</xsl:when>
<xsl:when test="../@class = 'org.apache.log4j.net.SocketAppender' or ../@class = 'org.apache.log4j.net.SyslogAppender'">
<xsl:comment> this is NOT needed tor this logger, so it is commented out </xsl:comment>
<xsl:comment><![CDATA[
<layout>
<pattern>]]><xsl:value-of select="param[@name = 'ConversionPattern']/@value"/><![CDATA[</pattern>
</layout>]]>
</xsl:comment>
</xsl:when>
<xsl:otherwise>
<layout>
<pattern><xsl:value-of select="param[@name = 'ConversionPattern']/@value"/></pattern>
</layout>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">Unknown layout class: <xsl:value-of select="@class"/></xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="filter">
<xsl:choose>
<xsl:when test="@class = 'org.apache.log4j.varia.LevelRangeFilter' and param[@name = 'LevelMin']/@value != '' and param[@name = 'LevelMax']/@value = 'FATAL'">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level><xsl:value-of select="param[@name = 'LevelMin']/@value"/></level>
</filter>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">Don't know what to do with filter</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="logger">
<logger>
<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
<xsl:attribute name="level">
<xsl:choose>
<xsl:when test="level/@value = 'FATAL'">OFF</xsl:when>
<xsl:otherwise><xsl:value-of select="level/@value"/></xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="@additivity != ''">
<xsl:attribute name="additivity"><xsl:value-of select="@additivity"/></xsl:attribute>
</xsl:if>
<xsl:apply-templates select="appender-ref"/>
</logger>
</xsl:template>
<xsl:template match="appender-ref">
<appender-ref>
<xsl:attribute name="ref"><xsl:value-of select="@ref"/></xsl:attribute>
</appender-ref>
</xsl:template>
<xsl:template match="root">
<xsl:call-template name="newline"/>
<root>
<xsl:attribute name="level"><xsl:value-of select="level/@value"/></xsl:attribute>
<xsl:apply-templates select="appender-ref"/>
<xsl:apply-templates select="comment()"/>
</root>
</xsl:template>
<xsl:template match="comment()">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template name="newline">
<!-- don't reformat this! -->
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>