Add logger functionality plus API upgrade

This commit is contained in:
wim
2021-10-11 13:20:43 +01:00
parent 029b76e2a0
commit 09578ffcc2
7 changed files with 37 additions and 98 deletions
@@ -32,7 +32,6 @@ public with sharing class fflib_LogEntry
public String className { get; set; }
public String executionContext { get; set; }
public Decimal lineNumber { get; set; }
public Id logEntryId { get; set; }
public System.LoggingLevel logLevel { get; set; }
public String message { get; set; }
public String methodName { get; set; }
@@ -57,8 +56,11 @@ public with sharing class fflib_LogEntry
this.className = logEvent.ClassName__c;
this.executionContext = logEvent.ExecutionContext__c;
this.lineNumber = logEvent.LineNumber__c;
this.logEntryId = logEvent.Id;
this.logLevel = System.LoggingLevel.valueOf(logEvent.LogLevel__c);
this.message = logEvent.Message__c;
this.methodName = logEvent.MethodName__c;
this.userId = logEvent.CreatedById;
this.createdDatetime = logEvent.CreatedDate;
}
public fflib_LogEvent__e toPlatformEvent()
@@ -47,25 +47,8 @@ public abstract with sharing class fflib_LogEventDomain extends fflib_PlatformEv
for (SObject record : Records)
{
this.logEntries.add(
new LogEntry((fflib_LogEvent__e) record)
new fflib_LogEntry((fflib_LogEvent__e) record)
);
}
}
public class LogEntry
{
public Id createdById;
public Datetime createdDatetime;
public System.LoggingLevel loggingLevel;
public String message;
public LogEntry(fflib_LogEvent__e event)
{
this.loggingLevel = System.LoggingLevel.values().get((Integer) event.LogLevel__c);
this.message = event.message__c;
this.createdById = event.CreatedById;
this.createdDatetime = event.CreatedDate;
}
}
}
@@ -30,7 +30,6 @@
*/
public with sharing class fflib_LogEventStoreListener extends fflib_LogEventDomain
{
/**
* Class constructor
*/
@@ -42,13 +41,17 @@ public with sharing class fflib_LogEventStoreListener extends fflib_LogEventDoma
public void run()
{
List<fflib_LogEntry__c> logEntryRecords = new List<fflib_LogEntry__c>();
for (fflib_LogEventDomain.LogEntry logEntry : logEntries)
for (fflib_LogEntry logEntry : logEntries)
{
logEntryRecords.add(
new fflib_LogEntry__c(
LogLevel__c = logEntry.loggingLevel.name(),
ClassName__c = logEntry.className,
ExecutionContext__c = logEntry.executionContext,
LineNumber__c = logEntry.lineNumber,
LogLevel__c = logEntry.logLevel.name(),
Message__c = logEntry.message,
User__c = logEntry.createdById
MethodName__c = logEntry.methodName,
User__c = logEntry.userId
)
);
}
@@ -50,7 +50,6 @@ public with sharing class fflib_Logger implements fflib_ILogger
public void add(String message)
{
add(System.LoggingLevel.INFO, message);
}
public void add(LoggingLevel loggingLevel, Exception e)
@@ -44,7 +44,7 @@ private class fflib_LogEventAppenderTest
Boolean caughtException = false;
try
{
eventAppender.append(System.LoggingLevel.ERROR, TEST_ERROR_MESSAGE);
eventAppender.append(new fflib_LogEntry(System.LoggingLevel.ERROR, TEST_ERROR_MESSAGE));
}
catch (Exception e)
{
@@ -55,19 +55,21 @@ private class fflib_LogEventAppenderTest
System.assert(!caughtException);
}
static testMethod void itShouldNotAppendInfoMessageWhileDefaultLevelIsSetToError()
@IsTest
static void itShouldNotAppendInfoMessageWhileDefaultLevelIsSetToError()
{
// GIVEN - And appender with a default level set to Error
fflib_LogEventAppender eventAppender = new fflib_LogEventAppender();
// WHEN - We append an Info Entry
eventAppender.append(System.LoggingLevel.INFO, TEST_ERROR_MESSAGE);
eventAppender.append(new fflib_LogEntry(System.LoggingLevel.INFO, TEST_ERROR_MESSAGE));
// THEN - It should be ignored
System.assertEquals(0, Limits.getDmlStatements(), 'Oops, the log entry should have been ignored');
}
static testMethod void itShouldAppendInfoMessageWhenTheApexLevelIsSetToInfo()
@IsTest
static void itShouldAppendInfoMessageWhenTheApexLevelIsSetToInfo()
{
// GIVEN - And appender with a Apex level set to Info
fflib_LogEventAppender eventAppender = new fflib_LogEventAppender();
@@ -75,14 +77,15 @@ private class fflib_LogEventAppenderTest
// WHEN - We append an Info Entry
System.Test.startTest();
eventAppender.append(System.LoggingLevel.INFO, TEST_ERROR_MESSAGE);
eventAppender.append(new fflib_LogEntry(System.LoggingLevel.INFO, TEST_ERROR_MESSAGE));
System.Test.stopTest();
// THEN - It should be ignored
System.assertEquals(1, [SELECT COUNT() FROM fflib_LogEntry__c]);
}
static testMethod void itShouldAppendInfoMessageWhenTheOrgSettingsLevelIsSetToInfo()
@IsTest
static void itShouldAppendInfoMessageWhenTheOrgSettingsLevelIsSetToInfo()
{
// GIVEN - And appender with a Org level set to Info
fflib_LogEventAppender eventAppender = new fflib_LogEventAppender();
@@ -94,7 +97,7 @@ private class fflib_LogEventAppenderTest
// WHEN - We append an Info Entry
System.Test.startTest();
eventAppender.append(System.LoggingLevel.INFO, TEST_ERROR_MESSAGE);
eventAppender.append(new fflib_LogEntry(System.LoggingLevel.INFO, TEST_ERROR_MESSAGE));
System.Test.stopTest();
// THEN - It should be ignored
@@ -79,7 +79,7 @@ private class fflib_LogEventStoreListenerTest
private static fflib_LogEvent__e generateLoggingLevelEvent(LoggingLevel loggingLevel, String message)
{
return new fflib_LogEvent__e(
LogLevel__c = loggingLevel.ordinal(),
LogLevel__c = loggingLevel.name(),
message__c = message
);
}
@@ -31,7 +31,7 @@
@IsTest
private class fflib_LoggerTest
{
/*
private static final List<String> ARGUMENTS = new List<String> {'A', 'B', 'C'};
private static final String ERROR_MESSAGE = 'Some Error';
private static final String ERROR_MESSAGE_WITH_ARGS = 'Some Error {0}, {1}, {2}';
@@ -247,80 +247,29 @@ private class fflib_LoggerTest
}
private class LogAppenderMock implements fflib_ILogAppender
private virtual class LogAppenderMock implements fflib_ILogAppender
{
private Map<System.LoggingLevel, List<String>> appendedMessages =
new Map<System.LoggingLevel, List<String>>();
protected List<fflib_LogEntry> appendedEntries = new List<fflib_LogEntry>();
public void append(String message)
public void append(fflib_LogEntry logEntry)
{
if (!this.appendedMessages.containsKey(System.LoggingLevel.INFO))
{
this.appendedMessages.put(System.LoggingLevel.INFO, new List<String>());
this.appendedEntries.add(logEntry);
}
appendedMessages.get(System.LoggingLevel.INFO).add(message);
}
public void append(LoggingLevel loggingLevel, String message)
{
if (!this.appendedMessages.containsKey(loggingLevel))
{
this.appendedMessages.put(loggingLevel, new List<String>());
}
appendedMessages.get(loggingLevel).add(message);
}
public List<String> getAppendedMessages(System.LoggingLevel loggingLevel)
{
if (this.appendedMessages.containsKey(loggingLevel))
List<String> result = new List<String>();
for (fflib_LogEntry logEntry : appendedEntries)
{
return this.appendedMessages.get(loggingLevel);
}
else
if (logEntry.logLevel == loggingLevel)
{
return new List<String>();
result.add(logEntry.message);
}
}
return result;
}
}
private class SecondLogAppenderMock implements fflib_ILogAppender
{
private Map<System.LoggingLevel, List<String>> appendedMessages =
new Map<System.LoggingLevel, List<String>>();
private class SecondLogAppenderMock extends LogAppenderMock { }
public void append(String message)
{
if (!this.appendedMessages.containsKey(System.LoggingLevel.INFO))
{
this.appendedMessages.put(System.LoggingLevel.INFO, new List<String>());
}
appendedMessages.get(System.LoggingLevel.INFO).add(message);
}
public void append(LoggingLevel loggingLevel, String message)
{
if (!this.appendedMessages.containsKey(loggingLevel))
{
this.appendedMessages.put(loggingLevel, new List<String>());
}
appendedMessages.get(loggingLevel).add(message);
}
public List<String> getAppendedMessages(System.LoggingLevel loggingLevel)
{
if (this.appendedMessages.containsKey(loggingLevel))
{
return this.appendedMessages.get(loggingLevel);
}
else
{
return new List<String>();
}
}
}
private class LogTestMockException extends Exception
{
}
*/
private class LogTestMockException extends Exception {}
}