|
| 1 | +/* |
| 2 | + * Copyright (c) 2018-2025, NWO-I CWI and Swat.engineering |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | +package org.rascalmpl.vscode.lsp.log; |
| 28 | + |
| 29 | +import java.net.URI; |
| 30 | + |
| 31 | +import org.apache.logging.log4j.Level; |
| 32 | +import org.apache.logging.log4j.core.LoggerContext; |
| 33 | +import org.apache.logging.log4j.core.appender.ConsoleAppender; |
| 34 | +import org.apache.logging.log4j.core.config.Configuration; |
| 35 | +import org.apache.logging.log4j.core.config.ConfigurationFactory; |
| 36 | +import org.apache.logging.log4j.core.config.ConfigurationSource; |
| 37 | +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder; |
| 38 | +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; |
| 39 | +import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration; |
| 40 | + |
| 41 | +public class LogJsonConfiguration extends ConfigurationFactory { |
| 42 | + |
| 43 | + @Override |
| 44 | + protected String[] getSupportedTypes() { |
| 45 | + return new String[] {"*"}; |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + @Override |
| 51 | + public Configuration getConfiguration(LoggerContext loggerContext, String name, URI configLocation) { |
| 52 | + return buildConfiguration(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Configuration getConfiguration(LoggerContext loggerContext, String name, URI configLocation, |
| 57 | + ClassLoader loader) { |
| 58 | + return buildConfiguration(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { |
| 63 | + return buildConfiguration(); |
| 64 | + } |
| 65 | + |
| 66 | + private Configuration buildConfiguration() { |
| 67 | + Level targetLevel = Level.getLevel(System.getProperty("log4j2.level", "INFO")); |
| 68 | + if (targetLevel == null) { |
| 69 | + targetLevel = Level.INFO; |
| 70 | + } |
| 71 | + |
| 72 | + ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder(); |
| 73 | + builder.setConfigurationName("JsonLogger"); |
| 74 | + builder.setStatusLevel(targetLevel); |
| 75 | + |
| 76 | + builder.add(builder |
| 77 | + .newAppender("Console", ConsoleAppender.PLUGIN_NAME) |
| 78 | + .addAttribute("target", ConsoleAppender.Target.SYSTEM_ERR) |
| 79 | + .add(builder.newLayout("JsonTemplateLayout") |
| 80 | + /* The JSON template has a max length (buffer size) of 8192 by default: |
| 81 | + https://logging.apache.org/log4j/2.x/manual/systemproperties.html#log4j2.encoderByteBufferSize |
| 82 | + If JSON documents are longer, the buffer will be flushed in between, |
| 83 | + and the JSON arrives truncated on the client side. To prevent his, we cap the max message length. */ |
| 84 | + .addAttribute("maxStringLength", 6000) |
| 85 | + .addAttribute("eventTemplateUri", "classpath:JsonLogTemplate.json") |
| 86 | + ) |
| 87 | + ); |
| 88 | + |
| 89 | + builder.add(builder |
| 90 | + .newRootLogger(targetLevel) |
| 91 | + .add(builder.newAppenderRef("Console")) |
| 92 | + ); |
| 93 | + |
| 94 | + return builder.build(); |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments