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 8272045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:00:37+00:00 2026-06-08T07:00:37+00:00

Does this code write to both a log file and the console at the

  • 0

Does this code write to both a log file and the console at the same time?

logFile = open("logfile.log",a)
print >>logFile,message
logFile.close()
  • 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-08T07:00:38+00:00Added an answer on June 8, 2026 at 7:00 am

    No, it will not write to both. print() will write to the console only. One quick note on your original code. I presume you define message somewhere, but the code is still incorrect. You need quotes around the a in the open statement, like this:

    open("logfile.log", "a")
    

    since I presume you meant to append to the file. Otherwise, you code throws a NameError since a is not a defined variable.

    However, as others have said, you should strongly consider using the logging module. Here is a simple example of how to write to both the console and a log file. The code is partially derived from here and here:

    import inspect
    import logging
    
    def function_logger(file_level, console_level = None):
        function_name = inspect.stack()[1][3]
        logger = logging.getLogger(function_name)
        logger.setLevel(logging.DEBUG) #By default, logs all messages
    
        if console_level != None:
            ch = logging.StreamHandler() #StreamHandler logs to console
            ch.setLevel(console_level)
            ch_format = logging.Formatter('%(asctime)s - %(message)s')
            ch.setFormatter(ch_format)
            logger.addHandler(ch)
    
        fh = logging.FileHandler("{0}.log".format(function_name))
        fh.setLevel(file_level)
        fh_format = logging.Formatter('%(asctime)s - %(lineno)d - %(levelname)-8s - %(message)s')
        fh.setFormatter(fh_format)
        logger.addHandler(fh)
    
        return logger
    
    def f1():
        f1_logger = function_logger(logging.DEBUG, logging.ERROR)
        f1_logger.debug('debug message')
        f1_logger.info('info message')
        f1_logger.warn('warn message')
        f1_logger.error('error message')
        f1_logger.critical('critical message')
    
    def f2():
        f2_logger = function_logger(logging.WARNING)
        f2_logger.debug('debug message')
        f2_logger.info('info message')
        f2_logger.warn('warn message')
        f2_logger.error('error message')
        f2_logger.critical('critical message')
    
    def main():
        f1()
        f2()
        logging.shutdown()
    
    main()
    

    Since logger objects can have more than one handler, we can create multiple handlers that write to different places. In my code, the function_logger function creates a logger object specific to the function in which it’s called.

    The function f1() logs DEBUG level messages and above to a file f1.log, while writing ERROR level messages and above to the console, with different formatting for each.

    The function f2(), however, logs nothing to the console and only logs WARNING level messages to its log file f2.log. Running this script once yields this output on the console:

    2012-07-20 10:46:38,950 - f1  - error message
    2012-07-20 10:46:38,953 - f1  - critical message
    

    and this output in f1.log and f2.log, respectively:

    f1.log:

    2012-07-20 10:46:38,950 - 26 - DEBUG    - debug message
    2012-07-20 10:46:38,950 - 27 - INFO     - info message
    2012-07-20 10:46:38,950 - 28 - WARNING  - warn message
    2012-07-20 10:46:38,950 - 29 - ERROR    - error message
    2012-07-20 10:46:38,953 - 30 - CRITICAL - critical message
    

    f2.log

    2012-07-20 10:46:38,960 - 36 - WARNING  - warn message
    2012-07-20 10:46:38,960 - 37 - ERROR    - error message
    2012-07-20 10:46:38,960 - 38 - CRITICAL - critical message
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have write this code but it does work only if I am already
Does this code always evaluate to false? Both variables are two's complement signed ints.
Why does this code not print an exception stack trace? public class Playground {
Does this code cause a memory leak: int main(){ int * a = new
Does this code work across all standard compliant C++ compilers (it works with g++)?
How does this code work to Find the next highest power of 2 for
Why does this code throw an Invalid Operation Exception? private SqlCommand cmd; // initialized
Why does this code int (*g)(int); int (*h)(char); h = g; In C, give
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
why does this code crash? is using strcat illegal on character pointers? #include <stdio.h>

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.