Skip to content

Commit

Permalink
Merge pull request #193 from jpedroh/feat-add-log4j
Browse files Browse the repository at this point in the history
feat: Add logging with log4j
  • Loading branch information
pauloborba authored Jun 21, 2024
2 parents 6266f6e + eaf903d commit c0c473f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ the Mining Framework take an input csv file and a name for the output dir
be used in the analysis. Required for (and
only considered when) running studies with
the CSDiff tool. Default: '{ } ( ) ; ,'
-log,--log-level <log level Specify the minimum log level: (OFF, FATAL,
ERROR, WARN, INFO, DEBUG, TRACE, ALL).
Default: "INFO"
-p,--push <link> Specify a git repository to upload the
output in the end of the analysis (format
https://github.com/<owner>/<name>
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
testCompileOnly 'junit:junit:4.12'
testRuntimeOnly 'org.junit.vintage:junit-vintage-engine:5.3.1'
implementation 'org.apache.logging.log4j:log4j-api:2.23.1'
implementation 'org.apache.logging.log4j:log4j-core:2.23.1'
}
6 changes: 5 additions & 1 deletion src/main/app/Main.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import arguments.Arguments
import arguments.InputParser

import exception.InvalidArgsException
import org.apache.logging.log4j.LogManager
import project.Project

import util.FileManager

class Main {
private static LOG = LogManager.getLogger(Main.class)

static main(args) {
ArgsParser argsParser = new ArgsParser()
try {
Arguments appArguments = argsParser.parse(args)

LOG.trace("Successfully parsed CLI args")

if (appArguments.isHelp()) {
argsParser.printHelp()
} else {
Expand Down
6 changes: 6 additions & 0 deletions src/main/arguments/ArgsParser.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package arguments
import exception.InvalidArgsException
import groovy.cli.commons.CliBuilder
import groovy.cli.commons.OptionAccessor
import org.apache.logging.log4j.Level

import java.text.ParseException
import java.text.SimpleDateFormat
Expand Down Expand Up @@ -33,6 +34,7 @@ class ArgsParser {
this.cli.k(longOpt: 'keep-projects', argName: 'keep projects', 'Specify that cloned projects must be kept after the analysis (those are kept in clonedRepositories/ )')
this.cli.e(longOpt: 'extension', args: 1, argName: 'file extenson', 'Specify the file extension that should be used in the analysis (e.g. .rb, .ts, .java, .cpp. Default: .java)')
this.cli.l(longOpt: 'language-separators', args: 1, argName: 'language syntactic separators', 'Specify the language separators that should be used in the analysis. Required for (and only considered when) running studies with the CSDiff tool. Default: \"{ } ( ) ; ,\"')
this.cli.log(longOpt: 'log-level', args: 1, argName: 'log level', 'Specify the minimum log level: (OFF, FATAL, ERROR, WARN, INFO, DEBUG, TRACE, ALL). Default: \"INFO\"')
}

Arguments parse(args) {
Expand Down Expand Up @@ -124,6 +126,10 @@ class ArgsParser {
if(this.options.l) {
args.setLanguageSyntacticSeparators(this.options.l)
}

if(this.options.log) {
args.setLogLevel(Level.toLevel(this.options.log))
}
}

private boolean repositoryExists(String repositoryURL) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/arguments/Arguments.groovy
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package arguments

import injectors.StaticAnalysisConflictsDetectionModule
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.core.config.Configurator

class Arguments {

Expand All @@ -16,6 +18,7 @@ class Arguments {
private boolean keepProjects
private String syntacticSeparators
private String fileExtension
private Level logLevel

Arguments() { // set the default values for all parameters
isHelp = false
Expand All @@ -29,6 +32,7 @@ class Arguments {
keepProjects = false
syntacticSeparators = '{ } ( ) ; ,'
fileExtension = 'java'
logLevel = Level.INFO
}

void setNumOfThreads (int numOfThreads) {
Expand Down Expand Up @@ -135,4 +139,12 @@ class Arguments {
return !resultsRemoteRepositoryURL.equals('')
}

Level getLogLevel() {
return logLevel
}

void setLogLevel(Level logLevel) {
this.logLevel = logLevel
Configurator.setRootLevel(logLevel)
}
}
8 changes: 8 additions & 0 deletions src/main/resources/log4j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Root Logger
rootLogger=info, STDOUT

# Direct log messages to stdout
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

0 comments on commit c0c473f

Please sign in to comment.