sematext/sematext-logsene-android

Can `logsene` object be used as a static variable?

theHilikus opened this issue · 7 comments

This is more of a design question

Is it possible to use logsene like other logging frameworks? There's a design problem with the demo application provided: it constructs a new Logsene object on the activity, this means that it's hard to log in other classes that are not activities.

For a logging library, what's common is to be able to use static methods to log: Example in crashlytics, example in Timber. They all use static methods to log.
Is there any way to accomplish this with Logsene? what i tried was creating a class like this

public class Log {

    private static final FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
    private static Logsene logsene;

    public static void initLogsene(Context context) {
        logsene = new Logsene(context);
    }

    public static void e(String tag, String message, Throwable exception) {
        android.util.Log.e(tag, message, exception);
        crashlytics.log(message);

        if (logsene != null) {
            logsene.error(tag + ':' + message);
            logsene.error(exception);
        }
    }

But this seems to create a memory leak due to the static Logsene field, which holds a reference to the context passed in the constructor. The warning is

Do not place Android context classes in static fields (static reference to Logsene which has field context pointing to Context); this is a memory leak

What is the recommended way here? how can I log from a class that doesn't have a context available?

gr0 commented

Hi,

You are right, at the moment context is passed and it may result in memory leak. I can try pushing some changes to allow static usage of the Logsene logger. Will you be able to wait for a few days, so I can find some time to make that happen?

otisg commented

In the mean time, @theHilikus, if you happen to have a local change that you think would be good for a PR, please submit it.

thank you @gr0
yes, i can test something if you need

@otisg unfortunately, I looked at the logsene code and i couldn't think of a way to make it work without the context, so no, i don't have any changes to propose. sorry

gr0 commented

I have a task on me in the next few days to look into such possibility. I can't promise that I will be able to do the figuring out and development quick, but will try. Thanks @theHilikus for bringing up that btw :)

gr0 commented

Hi @theHilikus - the 3.0.0 version of the library was released - https://github.com/sematext/sematext-logsene-android/releases/tag/3.0.0. Have a look at it and at the README on how to use the Logsene object in a static way. You can also look into upgrade instructions on how to migrate from 2.x :) Let us know if you have any more questions.

Hi @gr0 , i don't know why i missed this notification. I just tried it and it works great! thank you very much. it will be much easier to use now

gr0 commented

Great to hear that :)