question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Writing Logs to external file/s

See original GitHub issue

Expected Behavior

We used to be able to separate output log, info & exception info to different logs (with for example supervisor) std_err & std_out.

while true; do
pkill -f pokecli
eval "python pokecli.py --config configs/config.json >> logs/log.txt" &
eval "python pokecli.py --config configs/config1.json >> logs/log1.txt" &
sleep 1800
done

I’m using this bash script for restarting the bots every 30 minutes. It used to keep the logs in the separate files but i guess after the changes to the logging logic it doesn’t do the same. I call my bash script with this command. “nohup ./run.sh >> logs/restart.txt &” it was logging the exceptions into “restart.txt” and other stuff into respective log files but not anymore. Everything goes into restart.txt right now.

Can we add a config option like “pathToLogFile: ‘./logs/log.txt’” and keep the logs in there? It may work as now when this config is not specified or false.

Edited by mod: To clarify other user wanted features

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
IceBreezecommented, Aug 16, 2016

Yes I can, but not right now. I think I can do it in a few hours.

1reaction
IceBreezecommented, Aug 16, 2016

I use this in pokecli.py:

from datetime import timedelta, datetime
[...]
# set up logging to file (filename: "bot_YYYYMMDD.log")
logging.basicConfig(
    filename=datetime.now().strftime('bot_%Y%m%d.log'),
    level=logging.INFO,
    format='%(asctime)s [%(levelname).3s] [%(name)10s] %(message)s')
logger = logging.getLogger('cli')
logger.setLevel(logging.INFO)

# set up logging to console
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s [%(levelname).3s] [%(name)10s] %(message)s')
console.setFormatter(formatter)
# add console to root logger
logging.getLogger('').addHandler(console)

These settings log bot to console and screen, rotating the file daily.

It also changes the formatter from

'%(asctime)s [%(name)10s] [%(levelname)s] %(message)s'

to

'%(asctime)s [%(levelname).3s] [%(name)10s] %(message)s'

resulting in this type of log:

2016-01-01 01:20:01,051 [INF] [PokemonCatchWorker] [threw_pokeball] Used Pokeball, with chance 100.00 (14 left)
2016-01-01 01:20:02,052 [INF] [PokemonCatchWorker] [pokemon_caught] Captured Pidgey! [CP 20] [Potential 0.31] [5/7/2] [+100 exp]
2016-01-01 01:20:03,053 [INF] [PokemonCatchWorker] [gained_candy] You now have 123 Pidgey candy!
2016-01-01 01:20:04,054 [INF] [MoveToFort] [moving_to_fort] Moving towards pokestop XXXXXXXXXXXXXXXXXXXX - 0.06km
2016-01-01 01:20:05,055 [INF] [NicknamePokemon] [rename_pokemon] Pokemon Pidgey renamed to Pidgey031

The drawback is that you get color codes inside the log file, but i like it when I need to “cat” or “less -R” the log.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write logs to a file in Spring Boot - Tutorial Works
In this article I'm going to cover the quickest way to do write logs to a file, with minimum configuration. This means using...
Read more >
How to write only application logs to an external file in android
I am trying to write the logs of my application to an external file. My logs are like Log.e("Offset",""+mOffset); I am using the...
Read more >
How To Write Logs To A File With Python? - Better Stack
You need to use urllib3 logger and set the log level to DEBUG: python log = logging. getLogger('urllib3') log. setLevel(logging. DEBUG) To ...
Read more >
Log4j2 Tutorial 4 – Print Logs In External File Using XML ...
Introduction. In the previous post, we have learned to configure an XML file to print logs in a console i.e. console appender.
Read more >
How to get Exception log from a console and write it to ...
This method accepts an exception object, and appends it to a file using the write() method of the Files class.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found