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

CLI Environment - more useful log file #25180

Merged
merged 2 commits into from
Oct 9, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -50,6 +47,8 @@
import org.glassfish.api.admin.CommandValidationException;
import org.glassfish.api.admin.InvalidCommandException;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
* CLI Utility class
*/
Expand Down Expand Up @@ -248,14 +247,9 @@ public static void writeCommandToDebugLog(String cname, Environment env, String[
return;
}

BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(log, Charset.defaultCharset(), true));
DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date date = new Date();
out.write(dateFormat.format(date));
try (BufferedWriter out = new BufferedWriter(new FileWriter(log, UTF_8, true))) {
out.write(Instant.now().toString());
out.write(" EXIT: " + exit);

out.write(" " + cname + " ");

if (args != null) {
Expand All @@ -273,21 +267,9 @@ public static void writeCommandToDebugLog(String cname, Environment env, String[
out.write(arg + " ");
}
}
out.write("\n");
} catch (IOException e) {
// It is just a debug file.
} finally {
if (out != null) {
try {
out.write("\n");
} catch (Exception e) {
// ignore
}
try {
out.close();
} catch (Exception e) {
// ignore
}
}
throw new IllegalStateException("Could not write to the asadmin log file " + log, e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -42,6 +42,7 @@ public final class Environment {
private boolean debug;
private boolean trace;
private File logfile;
private Formatter formatter;

/**
* Set the prefix for environment variables referenced from the system environment by Environment objects.
Expand Down Expand Up @@ -108,14 +109,14 @@ public Environment(boolean ignoreEnvironment) {
fname = System.getenv(logProp);
}
if (fname != null) {
File f = new File(fname);

try {
if ((f.exists() || f.createNewFile()) && f.isFile() && f.canWrite()) {
logfile = f;
}
} catch (IOException e) {
/* ignore */ }
logfile = prepareFile(new File(fname));
}
final String formatterClass = env.get(PREFIX + "LOG_FORMATTER");
try {
formatter = formatterClass == null ? null
: (Formatter) Class.forName(formatterClass).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new IllegalStateException("This is not a valid formatter: " + formatterClass, e);
}
}

Expand Down Expand Up @@ -220,12 +221,31 @@ public File getDebugLogfile() {
}

public Formatter getLogFormatter() {
final String formatterClass = env.get(PREFIX + "LOG_FORMATTER");
return formatter;
}


private static File prepareFile(File file) {
try {
return formatterClass == null ? null
: (Formatter) Class.forName(formatterClass).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new IllegalStateException("This is not a formatter: " + formatterClass, e);
if (file.isFile()) {
if (file.canWrite()) {
return file;
}
throw new IllegalStateException("The file already exists, but we cannot write to it: " + file);
}
final File dir = file.getCanonicalFile().getParentFile();
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new IllegalStateException("The directory " + dir
+ " doesn't exist and isn't even possible to create it. Output to file '" + file
+ "' is not possible.");
}
if (file.createNewFile()) {
return file;
}
// createNewFile returns false just if the file already existed.
throw new IllegalStateException("Seems somebody else created the file right now, was it intentional? " + file);
} catch (IOException e) {
throw new IllegalStateException("Could not create a log file: " + file, e);
}
}
}