Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:17:53+00:00 2026-06-06T09:17:53+00:00

To get the logcat from an Android device, I use adb shell logcat (also

  • 0

To get the logcat from an Android device, I use adb shell logcat (also with some grep filtering in the mix).

An example output is as follows:

E/com.samsung.app( 3739): [MSC]>>>...
I/InputDispatcher( 2882): Delivering ...
D/ProgramMonitor( 3770): START ...
D/ProgramMonitor( 3770): LAUNCHER_RESUME...
I/Debug   ( 3815): onReceive...
E/Debug   ( 3815): receive ...
D/ClientReporter( 3036): ...

My question is, how can I change the color of output, based on the initial letter of the log. So that “E” is printed in red, “D” in green and so on… Just similar to how the log is seen in Eclipse.

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:17:55+00:00Added an answer on June 6, 2026 at 9:17 am

    I use a python script I found here :

    Modifying the Android logcat stream for full-color debugging

    It’s really easy to use and to modify if you want.

    Complete source code :

    #!/usr/bin/python
    
    '''
        Copyright 2009, The Android Open Source Project
    
        Licensed under the Apache License, Version 2.0 (the "License"); 
        you may not use this file except in compliance with the License. 
        You may obtain a copy of the License at 
    
            http://www.apache.org/licenses/LICENSE-2.0 
    
        Unless required by applicable law or agreed to in writing, software 
        distributed under the License is distributed on an "AS IS" BASIS, 
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
        See the License for the specific language governing permissions and 
        limitations under the License.
    '''
    
    # script to highlight adb logcat output for console
    # written by jeff sharkey, http://jsharkey.org/
    # piping detection and popen() added by other android team members
    
    
    import os, sys, re, StringIO
    import fcntl, termios, struct
    
    # unpack the current terminal width/height
    data = fcntl.ioctl(sys.stdout.fileno(), termios.TIOCGWINSZ, '1234')
    HEIGHT, WIDTH = struct.unpack('hh',data)
    
    BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
    
    def format(fg=None, bg=None, bright=False, bold=False, dim=False, reset=False):
        # manually derived from http://en.wikipedia.org/wiki/ANSI_escape_code#Codes
        codes = []
        if reset: codes.append("0")
        else:
            if not fg is None: codes.append("3%d" % (fg))
            if not bg is None:
                if not bright: codes.append("4%d" % (bg))
                else: codes.append("10%d" % (bg))
            if bold: codes.append("1")
            elif dim: codes.append("2")
            else: codes.append("22")
        return "\033[%sm" % (";".join(codes))
    
    
    def indent_wrap(message, indent=0, width=80):
        wrap_area = width - indent
        messagebuf = StringIO.StringIO()
        current = 0
        while current < len(message):
            next = min(current + wrap_area, len(message))
            messagebuf.write(message[current:next])
            if next < len(message):
                messagebuf.write("\n%s" % (" " * indent))
            current = next
        return messagebuf.getvalue()
    
    
    LAST_USED = [RED,GREEN,YELLOW,BLUE,MAGENTA,CYAN,WHITE]
    KNOWN_TAGS = {
        "dalvikvm": BLUE,
        "Process": BLUE,
        "ActivityManager": CYAN,
        "ActivityThread": CYAN,
    }
    
    def allocate_color(tag):
        # this will allocate a unique format for the given tag
        # since we dont have very many colors, we always keep track of the LRU
        if not tag in KNOWN_TAGS:
            KNOWN_TAGS[tag] = LAST_USED[0]
        color = KNOWN_TAGS[tag]
        LAST_USED.remove(color)
        LAST_USED.append(color)
        return color
    
    
    RULES = {
        #re.compile(r"([\w\.@]+)=([\w\.@]+)"): r"%s\1%s=%s\2%s" % (format(fg=BLUE), format(fg=GREEN), format(fg=BLUE), format(reset=True)),
    }
    
    TAGTYPE_WIDTH = 3
    TAG_WIDTH = 20
    PROCESS_WIDTH = 8 # 8 or -1
    HEADER_SIZE = TAGTYPE_WIDTH + 1 + TAG_WIDTH + 1 + PROCESS_WIDTH + 1
    
    TAGTYPES = {
        "V": "%s%s%s " % (format(fg=WHITE, bg=BLACK), "V".center(TAGTYPE_WIDTH), format(reset=True)),
        "D": "%s%s%s " % (format(fg=BLACK, bg=BLUE), "D".center(TAGTYPE_WIDTH), format(reset=True)),
        "I": "%s%s%s " % (format(fg=BLACK, bg=GREEN), "I".center(TAGTYPE_WIDTH), format(reset=True)),
        "W": "%s%s%s " % (format(fg=BLACK, bg=YELLOW), "W".center(TAGTYPE_WIDTH), format(reset=True)),
        "E": "%s%s%s " % (format(fg=BLACK, bg=RED), "E".center(TAGTYPE_WIDTH), format(reset=True)),
    }
    
    retag = re.compile("^([A-Z])/([^\(]+)\(([^\)]+)\): (.*)$")
    
    # to pick up -d or -e
    adb_args = ' '.join(sys.argv[1:])
    
    # if someone is piping in to us, use stdin as input.  if not, invoke adb logcat
    if os.isatty(sys.stdin.fileno()):
        input = os.popen("adb %s logcat" % adb_args)
    else:
        input = sys.stdin
    
    while True:
        try:
            line = input.readline()
        except KeyboardInterrupt:
            break
    
        match = retag.match(line)
        if not match is None:
            tagtype, tag, owner, message = match.groups()
            linebuf = StringIO.StringIO()
    
            # center process info
            if PROCESS_WIDTH > 0:
                owner = owner.strip().center(PROCESS_WIDTH)
                linebuf.write("%s%s%s " % (format(fg=BLACK, bg=BLACK, bright=True), owner, format(reset=True)))
    
            # right-align tag title and allocate color if needed
            tag = tag.strip()
            color = allocate_color(tag)
            tag = tag[-TAG_WIDTH:].rjust(TAG_WIDTH)
            linebuf.write("%s%s %s" % (format(fg=color, dim=False), tag, format(reset=True)))
    
            # write out tagtype colored edge
            if not tagtype in TAGTYPES: break
            linebuf.write(TAGTYPES[tagtype])
    
            # insert line wrapping as needed
            message = indent_wrap(message, HEADER_SIZE, WIDTH)
    
            # format tag message using rules
            for matcher in RULES:
                replace = RULES[matcher]
                message = matcher.sub(replace, message)
    
            linebuf.write(message)
            line = linebuf.getvalue()
    
        print line
        if len(line) == 0: break
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For my android app development, I am trying to get logcat messages from my
How we can Read Activity name from logcat in Android? I used some code
My Android webview is crashing and the error dump from adb logcat tells me
I'm trying to get some unit tests up for my Android apps. I was
i get a valid fd object from a caller. How can i find out
I'm new to android. I have some java knowledge (not extensive), and I've done
I'm testing mobile Android devices and I would like to redirect the device log
I have a RESTful web service and I want to access it from Android.
I've been trying to use the Android CTS package on a copy of Android
I'm using the Android compatibility library, targeting Android 2.2. If I use ListFragment With

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.