4

Is there anyway to log system.debug message(apex code) for a managed package im using beta version. Note:i tried capturing log for a particular user in my organisation... I could view soql execution logs and other but I couldn't view the system.debug messages from the apex code

1
  • 1
    HI Saravanan, and welcome to SFSE! It appears that what you're asking for is not currently possible, so you should vote on the idea. In the meantime, you'll have to come up with an alternative method of logging, such as writing to a database field or sending an email.
    – sfdcfox
    Commented Oct 4, 2016 at 6:04

2 Answers 2

2

As sfdcfox has mentioned, this is currently not possible. System.debug information will not be populated in the managed package. However, there are some workarounds in this situation.

One solution is to have a custom debug log object and use a text area field to track down the information you want to debug. You can create a custom setting to enable and disable the creation of such objects. So whenever you need to debug, you just enable that custom setting and the debug information will be written into the custom object. I have used this way before and it proved to be working in managed packages.

0

This has been addressed by Lence, but I found this to be clearer if I provide some code example for this, so what you need is to create a new custom object such as Log__c (how to do this is by going to SETUP -> OBJECT MANAGER -> add a new object and call it Log, then go to Fields & Relationships and add what you need for logging e.g., Message, Log_Level, and TimeStamp). Then you need to create a new class like:

public with sharing class Logger {
    public static void logDebug(String message) {
        logMessage('DEBUG', message);
    }
    
    public static void logInfo(String message) {
        logMessage('INFO', message);
    }
    
    public static void logError(String message) {
        logMessage('ERROR', message);
    }
    
    private static void logMessage(String level, String message) {
        Log__c logRecord = new Log__c();
        logRecord.Log_Level__c = level;
        logRecord.Message__c = message;
        logRecord.Timestamp__c = Datetime.now();
        insert logRecord;
    }
}

now in your other apex classes you can simply use the static methods of this class to log your events, e.g.,

Logger.logDebug(log);

when you installed the package you can simply run a query to get your logs:

SELECT Name, Log_Level__c, Message__c, Timestamp__c, CreatedById 
                                      FROM Log__c 
                                      ORDER BY Timestamp__c DESC 
                                      LIMIT 100

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .