Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CBRD-24662] Add a LoggingThread to access javasp's log file by only one thread #4137

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/jsp/com/cubrid/jsp/LoggingThread.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
*
* Copyright (c) 2016 CUBRID Corporation.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
*/

package com.cubrid.jsp;

import java.io.IOException;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoggingThread extends Thread {
private final Logger logger = Logger.getLogger("com.cubrid.jsp");

private FileHandler logHandler = null;
private LinkedBlockingQueue<String> logQueue = new LinkedBlockingQueue<String>();
private Level logginLevel = Level.SEVERE;

public LoggingThread(String path) throws SecurityException, IOException {
super();
logHandler = new FileHandler(path, true);
logger.addHandler(logHandler);
}

@Override
public void run() {
while (Thread.interrupted() == false) {
try {
String logString = logQueue.take();
logger.log(logginLevel, logString);
} catch (InterruptedException e) {
break;
}
}

if (logHandler != null) {
try {
logHandler.close();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the queue is not empty, shouldn't we print it out and close()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be reached in the exit routine before the server exits via exit(0). At this time, all other threads have already terminated, so the producer of the log no longer exists.

logger.removeHandler(logHandler);
} catch (Throwable e) {
}
}
}

public void log(String str) {
try {
logQueue.put(str);
} catch (InterruptedException e) {
}
}
}
46 changes: 20 additions & 26 deletions src/jsp/com/cubrid/jsp/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.ServerSocket;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newsclub.net.unix.AFUNIXServerSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
Expand All @@ -62,6 +62,8 @@ public class Server {

private static Server serverInstance = null;

private static LoggingThread loggingThread = null;

private Server(
String name, String path, String version, String rPath, String uPath, String port)
throws IOException, ClassNotFoundException {
Expand All @@ -74,6 +76,16 @@ private Server(
ServerSocket serverSocket = null;
int port_number = Integer.parseInt(port);
try {
String logFilePath =
rootPath
+ File.separatorChar
+ LOG_DIR
+ File.separatorChar
+ serverName
+ "_java.log";
loggingThread = new LoggingThread(logFilePath);
loggingThread.start();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the file name determined only when it is first driven?
Isn't there a time when we need to make a new file like when it's over a certain size?

Copy link
Contributor

@beyondykk9 beyondykk9 Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The log file size is no limit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a specification that is already defined, the file name is determined. In logging.properties in the jsp folder, you can set an option to create a new log file when the log file grows to a certain size. This is not controlled in code.

if (OSValidator.IS_UNIX && port_number == -1) {
final File socketFile = new File(udsPath);
if (socketFile.exists()) {
Expand Down Expand Up @@ -107,6 +119,7 @@ private Server(
Class.forName("com.cubrid.jsp.jdbc.CUBRIDServerSideDriver");

getJVMArguments(); /* store jvm options */

} else {
/* error, serverSocket is not properly initialized */
System.exit(1);
Expand Down Expand Up @@ -175,6 +188,7 @@ public static int start(String[] args) {
public static void stop(int status) {
getServer().setShutdown();
getServer().stopSocketListener();
loggingThread.interrupt();
System.exit(status);
}

Expand All @@ -183,30 +197,10 @@ public static void main(String[] args) {
}

public static void log(Throwable ex) {
FileHandler logHandler = null;

try {
logHandler =
new FileHandler(
rootPath
+ File.separatorChar
+ LOG_DIR
+ File.separatorChar
+ serverName
+ "_java.log",
true);
logger.addHandler(logHandler);
logger.log(Level.SEVERE, "", ex);
} catch (Throwable e) {
} finally {
if (logHandler != null) {
try {
logHandler.close();
logger.removeHandler(logHandler);
} catch (Throwable e) {
}
}
}
StringWriter sw = new StringWriter();
ex.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString();
loggingThread.log(exceptionAsString);
}

public void setShutdown() {
Expand Down