Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a11a86f
produceLog now has error handling for null message and log factory no…
rhys-h-walker Jul 20, 2025
0afd01b
Added null checks and error handling to, FileManagement, Logger and L…
rhys-h-walker Jul 20, 2025
c525480
Error handling to initialization logic and changing visibility
rhys-h-walker Jul 20, 2025
523ed4f
Added error handling in constructors and in reading files
rhys-h-walker Jul 20, 2025
10fc07b
Updated readDirectiory to check permissions correctly, added initiali…
rhys-h-walker Jul 20, 2025
29deea8
Removed unecessary PrintLn
rhys-h-walker Jul 20, 2025
4952430
Updated logger now to match previous push, print codes in uppercase t…
rhys-h-walker Jul 20, 2025
d9912f9
Console output is now checked for correct colour codes, messages, tim…
rhys-h-walker Jul 20, 2025
f414ed6
Removed App causing failure in CI
rhys-h-walker Jul 20, 2025
452db33
Removed unused imports, updates Test directory structure
rhys-h-walker Jul 20, 2025
c720b42
Logger updated to not fail on initialization
rhys-h-walker Jul 20, 2025
91f3a47
Printed log messages will replace null with "null"
rhys-h-walker Jul 20, 2025
fd5e3a8
Updated README to match current project goals, detail testing framewo…
rhys-h-walker Jul 20, 2025
ec49f11
Move testing into it's own README
rhys-h-walker Jul 21, 2025
52ce76c
Added new TestFileManagement, cleans up after itself
rhys-h-walker Jul 21, 2025
67caf15
Add shutdown method to Logger, and cleanup after tests
rhys-h-walker Jul 21, 2025
c30bc66
Added additional logging capabilities
rhys-h-walker Jul 21, 2025
c1decda
Changed viewLogVisibility return type from boolean to Boolean
rhys-h-walker Jul 21, 2025
de436a7
Tests added for checking runtime configuration tests
rhys-h-walker Jul 21, 2025
5d4ec39
Added tests for Visibility maps correctness
rhys-h-walker Jul 21, 2025
52d52f5
Updates Testing README
rhys-h-walker Jul 21, 2025
f8da161
Console output has been tested to either appear or not appear based o…
rhys-h-walker Jul 21, 2025
e23e052
Invalid application names are now checked for
rhys-h-walker Jul 21, 2025
98fbdea
Test for "null" in console logging
rhys-h-walker Jul 21, 2025
830eacd
Added tests for invalid application Name
rhys-h-walker Jul 21, 2025
06d4c0a
Added tests for LogFactory creation, prepared tests README for release
rhys-h-walker Jul 21, 2025
ad583e5
Updated README ahead of v1.1.0 release
rhys-h-walker Jul 21, 2025
351951b
Updated POM ahead of release
rhys-h-walker Jul 21, 2025
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
37 changes: 23 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# OnRailsLogger

Welcome to OnRailsLogger a simple logging application which can fit right into your codebase. OnRails features total of 6 different log types, and 6 different logging levels. The application is simple requiring one initialization step at your codes entrypoint and then just report your error.
Welcome to OnRailsLogger a simple logging application which can fit right into your codebase. OnRails features total of 6 different log types, each of which is individually silenceable, or choose from three builtin configurations. The application is simple requiring one initialization step at your codes entrypoint and then just log whatever you need to.

**This is a simple logger, it has a synchronized block around the main logging method. This is the only thread safety included. No guarantees are given through locks/queues/semaphores.**

Expand All @@ -11,14 +11,14 @@ Logs can be found in your user.home directory under OnRailsLogging and then your
The logger is still in development. Simple things you may expect to exist may not at this point. They will come soon, checkout the roadmap at the bottom which details the next features to come.

## Adding the logger to a maven project
The logger uses JitPack to package and install the project. Paste the code found below into your pom.xml. **Check tags for releases, not all changes in main are in the current release VERSION: `v1.0.0`**
The logger uses JitPack to package and install the project. Paste the code found below into your pom.xml. **Check tags for releases, not all changes in main are in the current release VERSION: `v1.1.0`**.

```xml
<dependencies>
<dependency>
<groupId>com.github.rhys-h-walker</groupId>
<artifactId>OnRailsLogging</artifactId>
<version>Tag</version>
<version>V1.1.0</version>
</dependency>
<dependencies>

Expand All @@ -34,19 +34,16 @@ For additional instructions on how to install check out the jitpack page for the

## How to use

To use the logger you must first decide how you wish it to work. Currently there are three options:
- Console reporting
- Logging to file
- Both of the above
Before using the Logger there are two things to understand. You have the power to log to a file and console or just console. When logging to just console add true to the final argument of initializeLogger. Logging visibility can be customized, using a `HashMap<LoggingType, Boolean>`, this is the second argument in the second initializer. There are three available by default, check LoggingType for the methods.

To just log to a console ignore the initialization step. To just output to file set LoggingLevel.NONE in initialization.
Make sure to call initialize Logger before doing anything else, otherwise logs will only be shown on the console. Not calling `initializeLogger` has not been fully tested so behaviour may be random at times.

```Java
Logger.initializeLogger("applicationName");
Logger.initializeLogger("applicationName", false);

// Or

Logger.initializeLogger("applicationName", LoggingLevel.ALL);
Logger.initializeLogger("applicationName", LoggingType.defaultVisibility(), false);

// Then

Expand All @@ -56,6 +53,10 @@ Logger.loginfo("This is an info message");
Logger.logwarn("This is a warning message");
Logger.logdebug("This is a debug message");
Logger.logmiscellaneous("This is a miscellaneous message");

// Once completed

Logger.shutdown(); // Release any system resources being used
```

The output file will look like this:
Expand All @@ -75,15 +76,22 @@ When printing to console the outputs have colours like so: <br>
- Allow for logging to be more fine grained with level
- This will allow for the silencing of certain logging messages,
- Basic thread safety, synchronized block in produceLog
- Error handling for most cases
- Testing for basic console output
- Error handling in file-creation/string creation
- Handle null messages with a message when printing/writing
- Inputs are now screened in methods, errors reported
- Console only reporting is always active as a fallback for when file reporting breaks
- Added shutdown method to Logger
- Added tests for basic functionality: Check README in tests directory
- Application names should no longer break file systems. Still be careful as to what you name your app

## Roadmap

Updates are located in three groups, Next is what I am currently working on, soon will be after that and future has no specific date or timeframe attached to it.

### Next
- Add Junit tests for console output
- Error handling in file-creation/string creation
- Handle null messages with a message when printing/writing
- Testing via Junit: Check README in tests directory

### Soon
- Configuration file support:
Expand All @@ -101,9 +109,10 @@ Updates are located in three groups, Next is what I am currently working on, soo
- This is an embedable component
- Can be added anywhere and will just view logs for the app
- Custom log formats (Allow adjustment of certain features based on user requirements)
- Reinitialization of the Logger, recovery from a crash if told to be user

## Examples

OnRailsLogger is new and there are few examples, however I have converted one of my old projects over to use it, you can find it [here](https://github.com/rhys-h-walker/rambling-jesters/tree/adding-on-rails-logging).
OnRailsLogger is new and there are few examples, however I have converted one of my old projects over to use it, you can find it [here](https://github.com/rhys-h-walker/rambling-jesters/tree/adding-on-rails-logging) it is important to note that this uses v1.0.0 and not the most up to date version.

Please if you use this project contact me so I can place your project on examples.
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

<groupId>com.github.rhys-h-walker</groupId>
<artifactId>OnRailsLogging</artifactId>
<version>v1.0.0</version>
<version>v1.1.0</version>

<name>OnRailsLogging</name>
<!-- FIXME change it to the project's website -->
<url>https://github.com/rhys-h-walker/OnRailsLogging</url>

<properties>
Expand All @@ -34,7 +33,6 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<!-- Optionally: parameterized tests support -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
Expand All @@ -43,14 +41,12 @@
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<pluginManagement>
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.4.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.3.1</version>
Expand All @@ -75,7 +71,6 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.1</version>
Expand Down
19 changes: 0 additions & 19 deletions src/main/java/com/github/rhys_h_walker/App.java

This file was deleted.

93 changes: 65 additions & 28 deletions src/main/java/com/github/rhys_h_walker/FileManagement.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.util.ArrayList;

import com.github.rhys_h_walker.core_enums.LoggingType;
import com.github.rhys_h_walker.misc.Utilities;

/**
* A static class which implements most of the IO features required by OnRailsLogger
Expand All @@ -18,56 +20,91 @@ public class FileManagement {
/**
* Based on a timestamp either locate or create a file for logging
* @param timestamp The timestamp to use to find the log file
* @return
* @return The File object, null on any errors
*/
public static File locateFile(LocalDateTime ts, File applicationDirectory) {
// We have our timestamp in this format yyyy-MM-dd-HH:mm:ss
File loggingFile = new File(applicationDirectory.getAbsolutePath()+File.separator+ts.getYear()+File.separator+ts.getMonthValue()+File.separator+ts.getDayOfMonth()+File.separator+ts.getHour()+"_"+ts.getMinute()+"_"+ts.getSecond()+".log");

if (loggingFile.exists()){
return loggingFile;
} else {
File parentDirectory = loggingFile.getParentFile();
if (!parentDirectory.exists()) {
parentDirectory.mkdirs();

// Handle null cases for arguments
if (ts == null || applicationDirectory == null) {
System.err.println("timestamp is null or application directory is null");
return null;
}

try {
// We have our timestamp in this format yyyy-MM-dd-HH:mm:ss
File loggingFile = new File(Utilities.formatFilepathFromTimestamp(ts, applicationDirectory));

if (loggingFile.exists()){
return loggingFile;
} else {
File parentDirectory = loggingFile.getParentFile();
if (!parentDirectory.exists()) {
boolean success = parentDirectory.mkdirs();

// Handle failure of file creation gracefully
if (!success) {
System.err.println("Unsuccessfull creation of file: " + loggingFile.toString());
return null;
}
}
return loggingFile;
}
return loggingFile;
} catch (SecurityException e) {
System.err.println("No permission to create/access the directory requested\n" + e);
return null;
} catch (Exception e) {
System.err.println("General exception on creating/accessing the directory requested\n" + e);
return null;
}
}

public static void writeToLogFile(File logFile, String timestamp, LoggingType logType, String message, PrintWriter pw) {

if (pw == null) {
System.err.println("PrintWriter is null, returning without writing");
return;
}

String logMessage = "[" + timestamp + "] " + logType.toString() + ": " + message;
pw.println(logMessage);
pw.flush();
}

/**
* Read in a log file and return it split by line
* @param logFile
* @return
* @param logFile The File to be read from
* @return An ArrayList of Strings, null if error
*/
public static ArrayList<String> readLogFile(File logFile) {

if (!logFile.exists()) {
System.exit(1);
if (logFile == null) {
System.err.println("LogFile is null when trying to read");
return null;
}

ArrayList<String> lines = new ArrayList<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(logFile));
String line = reader.readLine();
while (line != null) {
lines.add(line);
line = reader.readLine();
}
reader.close();
if (!logFile.exists()) {
System.err.println("File does not exist, nothing to read");
return null;
}

return lines;
} catch(Exception e) {
// Failure detected error and then return null
System.err.println("Failed opening file " + e);
if (!logFile.canRead()) {
System.err.println("File cannot be read, incorrect permissions!");
return null;
}

ArrayList<String> logs = new ArrayList<>();

try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
String line;
while ((line = reader.readLine()) != null) {
logs.add(line);
}
} catch (IOException e) {
System.err.println("OnRailsLogger: Error reading log file: " + e.getMessage());
// Return partial results instead of crashing
}

return logs;

}
}
Loading