Git Product home page Git Product logo

Comments (4)

tony19 avatar tony19 commented on August 16, 2024

Actually, most (if not all) original Logback appenders should* work on Android except those that rely on JMS, JMX, JNDI, SMTP, and Servlets. (*: I haven't tested all of them)

Sure, you can add LogcatAppender.

from logback-android.

phreed avatar phreed commented on August 16, 2024

The following seems to do the trick.
That was too easy, I must have done something wrong?

/**
 * The appender wraps the native android logging mechanism.
 * 
 */
package ch.qos.logback.android;

import java.io.IOException;

import android.util.Log;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.AppenderBase;


public class LogcatAppender extends AppenderBase<ILoggingEvent> {

    private PatternLayoutEncoder encoder;

    public LogcatAppender() {
    }

    @Override
    public void start() {
        if (this.encoder == null) {
            addError("No layout set for the appender named ["+ name +"].");
            return;
        }

        try {
            this.encoder.init(System.out);
        } catch (IOException e) {
        }
        super.start();
    }

    public void append(ILoggingEvent event) {
        // output the events as formatted by our layout

        switch (event.getLevel().levelInt) {
        case Level.TRACE_INT: 
            Log.v(event.getLoggerName(), event.getFormattedMessage());
            return;
        case Level.DEBUG_INT:
            Log.d(event.getLoggerName(), event.getFormattedMessage());
            return;
        case Level.INFO_INT:
            Log.i(event.getLoggerName(), event.getFormattedMessage());
            return;
        case Level.WARN_INT:
            Log.w(event.getLoggerName(), event.getFormattedMessage());
            return;
        case Level.ERROR_INT:
            Log.d(event.getLoggerName(), event.getFormattedMessage());
            return;
        case Level.OFF_INT:
        default:
            return;
        }
    }

    public PatternLayoutEncoder getEncoder() {
        return this.encoder;
    }

    public void setEncoder(PatternLayoutEncoder encoder) {
        this.encoder = encoder;
    }

}

from logback-android.

tony19 avatar tony19 commented on August 16, 2024

Thanks for the contribution. I made slight modifications and put it into the latest build: commit 11708caa42

from logback-android.

lock avatar lock commented on August 16, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

from logback-android.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.