Skip to content

Commit

Permalink
CUSTCOM-265 Restarting the domain with the Payara log formatter cause…
Browse files Browse the repository at this point in the history
…s the log to rotate
  • Loading branch information
Cousjava authored and dmatej committed Apr 22, 2020
1 parent 07b10b0 commit ca93e01
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 5 deletions.
9 changes: 7 additions & 2 deletions nucleus/core/logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
-->

<!-- Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates] -->
<!-- Portions Copyright [2016-2020] [Payara Foundation and/or its affiliates] -->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
Expand Down Expand Up @@ -100,7 +100,12 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>


<dependency>
<groupId>org.glassfish</groupId>
<artifactId>jakarta.json</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2019] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2020] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.server.logging;

Expand All @@ -60,7 +60,6 @@
import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.ErrorManager;
import java.util.logging.Formatter;
Expand Down Expand Up @@ -201,6 +200,7 @@ public void postConstruct() {
String strLine;
int odlFormatter = 0;
int uniformLogFormatter = 0;
int jsonLogFormatter = 0;
int otherFormatter = 0;
boolean mustRotate = false;

Expand All @@ -213,6 +213,9 @@ public void postConstruct() {
} else if (LogFormatHelper.isODLFormatLogHeader(strLine)) {
// for ODL formatter
odlFormatter++;
} else if (LogFormatHelper.isJSONFormatLogHeader(strLine)) {
//for JSON Log format
jsonLogFormatter++;
} else {
otherFormatter++; // for other formatter
}
Expand All @@ -236,6 +239,8 @@ public void postConstruct() {
currentFileHandlerFormatter = "com.sun.enterprise.server.logging.ODLLogFormatter";
} else if (uniformLogFormatter > 0) {
currentFileHandlerFormatter = "com.sun.enterprise.server.logging.UniformLogFormatter";
} else if (jsonLogFormatter > 0) {
currentFileHandlerFormatter = "fish.payara.enterprise.server.logging.JSONLogFormatter";
}

String propertyValue = manager.getProperty(className + ".logtoFile");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2020] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.server.logging;

import java.io.StringReader;
import java.util.regex.Pattern;
import javax.json.Json;
import javax.json.JsonException;
import javax.json.JsonReader;

/**
* Helper class that provides methods to detect the log format of a record.
Expand Down Expand Up @@ -88,6 +92,21 @@ && countOccurrences(line, '[') > 4) {
return false;
}
}

/**
* Determines whether the given line is the beginning of a JSON log record.
* @param line String to test if json
* @return If the line is valid JSON
*/
public static boolean isJSONFormatLogHeader(String line) {
JsonReader reader = Json.createReader(new StringReader(line));
try {
reader.read();
return true;
} catch (JsonException ex ) {
return false;
}
}

private static int countOccurrences(String haystack, char needle) {
int count = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2020 Payara Foundation and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://github.com/payara/Payara/blob/master/LICENSE.txt
* See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* The Payara Foundation designates this particular file as subject to the "Classpath"
* exception as provided by the Payara Foundation in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.enterprise.server.logging;

import org.junit.Assert;
import org.junit.Test;

/**
* Test to ensure that the LogForatHelper correctly works out the log type
* @author jonathan coustick
*/
public class LogFormatTest {

private static final String JSON_RECORD = "{\"Timestamp\":\"2020-04-20T21:53:40.248+0100\",\"Level\":\"INFO\","
+ "\"Version\":\"Payara 5.202\",\"LoggerName\":\"javax.enterprise.logging\",\"ThreadID\":\"22\",\"ThreadName\":"
+ "\"RunLevelControllerThread-1587416020198\",\"TimeMillis\":\"1587416020248\",\"LevelValue\":\"800\","
+ "\"MessageID\":\"NCLS-LOGGING-00009\",\"LogMessage\":\"Running Payara Version: Payara Server 5.202"
+ " #badassfish (build ${build.number})\"}";
private static final String ODL_RECORD = "[2020-04-20T22:05:43.203+0100] [Payara 5.202] [INFO] [NCLS-LOGGING-00009] "
+ "[javax.enterprise.logging] [tid: _ThreadID=21 _ThreadName=RunLevelControllerThread-1587416743113] "
+ "[timeMillis: 1587416743203] [levelValue: 800] [[";
private static final String ULF_RECORD = "[#|2020-04-20T22:02:35.314+0100|INFO|Payara 5.202|javax.enterprise.logging|_ThreadID=21;"
+ "_ThreadName=RunLevelControllerThread-1587416555246;_TimeMillis=1587416555314;_LevelValue=800;"
+ "_MessageID=NCLS-LOGGING-00009;|";


@Test
public void JSONFormatTest() {
Assert.assertTrue(LogFormatHelper.isJSONFormatLogHeader(JSON_RECORD));
Assert.assertFalse(LogFormatHelper.isODLFormatLogHeader(JSON_RECORD));
Assert.assertFalse(LogFormatHelper.isUniformFormatLogHeader(JSON_RECORD));
}

@Test
public void ODLFormatTest() {
Assert.assertFalse(LogFormatHelper.isJSONFormatLogHeader(ODL_RECORD));
Assert.assertTrue(LogFormatHelper.isODLFormatLogHeader(ODL_RECORD));
Assert.assertFalse(LogFormatHelper.isUniformFormatLogHeader(ODL_RECORD));
}

@Test
public void UniformFormatTest() {
Assert.assertFalse(LogFormatHelper.isJSONFormatLogHeader(ULF_RECORD));
Assert.assertFalse(LogFormatHelper.isODLFormatLogHeader(ULF_RECORD));
Assert.assertTrue(LogFormatHelper.isUniformFormatLogHeader(ULF_RECORD));
}

}

0 comments on commit ca93e01

Please sign in to comment.