Skip to content

Commit

Permalink
Merge pull request #258 from laixintao/feature/log-config
Browse files Browse the repository at this point in the history
Support config log location in iredisrc.
  • Loading branch information
laixintao authored Feb 15, 2020
2 parents 329fa5c + 19e925f commit c95d69a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Feature: Support `EXIT` to exit iredis REPL.
* Feature: Support `CLEAR` to clear screen.
* Feature: Support config log location in iredisrc file, default to None.

### 0.9.1

Expand Down
12 changes: 1 addition & 11 deletions iredis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import os
import logging
from pathlib import Path

__version__ = "0.9.1"


logging.basicConfig(
filename=os.path.join(os.getenv("HOME"), ".iredis.log"),
filemode="a",
format="%(levelname)5s %(message)s",
level="DEBUG",
)
__version__ = "0.9.1"

logger = logging.getLogger(__name__)
logger.info("------ iRedis ------")

project_root = Path(os.path.dirname(os.path.abspath(__file__)))
project_data = project_root / "data"
10 changes: 4 additions & 6 deletions iredis/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self):
self.withscores = False
self.version = "Unknown"
self.no_version_reason = None
self.log_location = None

self.warning = True

Expand Down Expand Up @@ -96,17 +97,14 @@ def load_config_files(iredisrc):

config.raw = config_obj["main"].as_bool("raw")
config.completer_max = config_obj["main"].as_int("completer_max")
config.retry_times = config_obj["main"].as_int("retry_times")
config.newbie_mode = config_obj["main"].as_bool("newbie_mode")
config.rainbow = config_obj["main"].as_bool("rainbow")
config.retry_times = config_obj["main"].as_int("retry_times")
config.socket_keepalive = config_obj["main"].as_bool("socket_keepalive")
config.decode = config_obj["main"]["decode"]
config.no_info = config_obj["main"].as_bool("no_info")
config.bottom_bar = config_obj["main"].as_bool("bottom_bar")
config.warning = config_obj["main"].as_bool("warning")

logger.info(
f"[config] retry_times={config.retry_times}({type(config.retry_times)})."
)
config.decode = config_obj["main"]["decode"]
config.log_location = config_obj["main"]["log_location"]

return config_obj
4 changes: 4 additions & 0 deletions iredis/data/iredisrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ bottom_bar = True
# command, that may cause harm to the redis-server or hang server,
# such as "KEYS", "DEL" or "SHUTDOWN".
warning = True

# IRedis log for debugging, leave this black will disable log.
# eg. ~/.iredis.log
log_location = ~/.iredis1.log
15 changes: 13 additions & 2 deletions iredis/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,20 @@
from . import __version__

logger = logging.getLogger(__name__)

HISTORY_FILE = Path(os.path.expanduser("~")) / ".iredis_history"


def setup_log():
if config.log_location:
logging.basicConfig(
filename=os.path.expanduser(config.log_location),
filemode="a",
format="%(levelname)5s %(message)s",
level="DEBUG",
)
logger.info("------ iRedis ------")


def greetings():
iredis_version = f"iredis {__version__} (Python {platform.python_version()})"
if config.no_version_reason:
Expand Down Expand Up @@ -200,12 +210,13 @@ def gather_args(ctx, h, p, n, password, newbie, iredisrc, decode, raw, rainbow,
Type "help" in interactive mode for information on available commands
and settings.
"""
load_config_files(iredisrc)
setup_log()
logger.info(
f"[commandline args] host={h}, port={p}, db={n}, newbie={newbie}, "
f"iredisrc={iredisrc}, decode={decode}, raw={raw}, "
f"cmd={cmd}, rainbow={rainbow}."
)
load_config_files(iredisrc)
# raw config
if raw is not None:
config.raw = raw
Expand Down
25 changes: 25 additions & 0 deletions tests/cli_tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import pexpect
from textwrap import dedent
from pathlib import Path


def test_log_location_config():
config_content = dedent(
"""
[main]
log_location = /tmp/iredis1.log
"""
)
with open("/tmp/iredisrc", "w+") as etc_config:
etc_config.write(config_content)

cli = pexpect.spawn("iredis -n 15 --iredisrc /tmp/iredisrc", timeout=1)
cli.expect("127.0.0.1")
cli.close()

log = Path("/tmp/iredis1.log")
assert log.exists()
with open(log, "r") as logfile:
content = logfile.read()

assert len(content) > 100

0 comments on commit c95d69a

Please sign in to comment.