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

Add first implementation of Fox Logger #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions modules/fox-logging/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.ensolvers.fox-java</groupId>
<artifactId>main</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>

<artifactId>fox-logging</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Fox - Logger</name>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta4</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.newrelic.agent.java</groupId>
<artifactId>newrelic-api</artifactId>
<version>3.28.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* Copyright (c) 2021 Ensolvers
* All Rights Reserved
*
* The contents of this file is dual-licensed under 2 alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to the project.
*
* You may obtain a copy of the LGPL License at: http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at: http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/

package com.ensolvers.fox.logging;

import com.newrelic.api.agent.NewRelic;
import java.util.Collections;
import org.apache.log4j.Category;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.slf4j.helpers.MessageFormatter;

/** Provides utilities for adding logging with New Relic support. */
public class FoxLogger extends Category {
private Logger logger;

public static FoxLogger getLogger(String name) {
return new FoxLogger(LogManager.getLogger(name));
}

public FoxLogger(Logger logger) {
super(logger.getName());
this.logger = logger;
}

@Override
public void info(Object message) {
this.logger.info(message);
}

@Override
public void error(Object message) {
NewRelic.noticeError(String.valueOf(message));
this.logger.error(message);
}

public void error(String message) {
NewRelic.noticeError(message);
this.logger.error(message);
}

public void error(String message, Throwable t) {
NewRelic.noticeError(t, Collections.singletonMap("message", message));
this.logger.error(message, t);
}

public void error(String message, boolean doNewRelicLog) {
if (doNewRelicLog) {
NewRelic.noticeError(message);
}
this.logger.error(message);
}

public void error(Throwable t, boolean doNewRelicLog) {
if (doNewRelicLog) {
NewRelic.noticeError(t);
}
this.logger.error("UNHANDLED EXCEPTION", t);
}

@Override
public void warn(Object message) {
this.logger.warn(message);
}

@Override
public void warn(Object message, Throwable t) {
this.logger.warn(message, t);
}

@Override
public void debug(Object message) {
this.logger.debug(message);
}

@Override
public void fatal(Object message) {
this.logger.fatal(message);
}

/**
* Log a message at different levels according to the specified format and argument.
*
* @param format like "This {} is {} a text"
* @param args like "first word, another word"
*/

public void finfo(String format, Object... args) {
this.info(MessageFormatter.arrayFormat(format, args).getMessage());
}

public void ferror(String format, Throwable t, Object... args) {
this.error(MessageFormatter.arrayFormat(format, args).getMessage(), t);
}

public void ferrorWithoutException(String format, Object... args) {
this.error(MessageFormatter.arrayFormat(format, args).getMessage());
}

public void fwarn(String format, Object... args) {
this.warn(MessageFormatter.arrayFormat(format, args).getMessage());
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@
<module>modules/fox-alerts</module>
<module>modules/fox-quality</module>
<module>modules/fox-cli</module>
<module>modules/fox-logging</module>
</modules>

</project>