Simple log rotation scriptLocating matching files with input folder and file prefixBash Music PlayerSimple...
Why would a home insurer offer a discount based on credit score?
Is plausible to have subspecies with & without separate sexes?
What did the 8086 (and 8088) do upon encountering an illegal instruction?
Which are the methodologies for interpreting Vedas?
Approach sick days in feedback meeting
Is there a radar system monitoring the UK mainland border?
Can an open source licence be revoked if it violates employer's IP?
What's the difference between DHCP and NAT? Are they mutually exclusive?
What do I need to do, tax-wise, for a sudden windfall?
When a class dynamically allocates itself at constructor, why does stack overflow happen instead of std::bad_alloc?
As easy as Three, Two, One... How fast can you go from Five to Four?
Did I need a visa in 2004 and 2006?
How to import .txt file with missing data?
Part of my house is inexplicably gone
ISP is not hashing the password I log in with online. Should I take any action?
If absolute velocity does not exist, how can we say a rocket accelerates in empty space?
Can a 40amp breaker be used safely and without issue with a 40amp device on 6AWG wire?
Does the UK delegate some immigration control to the Republic of Ireland?
Harley Davidson clattering noise from engine, backfire and failure to start
Why didn't all the iron and heavier elements find their way to the center of the accretion disc in the early solar system?
Why did the Death Eaters wait to reopen the Chamber of Secrets?
Is this Homebrew Eldritch Invocation, Accursed Memory, balanced?
In The Incredibles 2, why does Screenslaver's name use a pun on something that doesn't exist in the 1950s pastiche?
What does BREAD stand for while drafting?
Simple log rotation script
Locating matching files with input folder and file prefixBash Music PlayerSimple Log HandlerRead decibel level from a USB meter, display it as a live visualization, and send it via FTPSimple log writer classWriting to rotating log file via channelsWrite a message to a logScraping metrics from log filesTrace route based on Cisco routing table text outputSimple server log backup script utilising AWS
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).
I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def rotate(self):
files = ["{}{}.{}".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]
[self.__touch(f) for f in files if not exists(f)]
current_log = "{}.{}".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return
files.insert(0, current_log)
for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])
self.__touch(files[0])
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()
python beginner python-3.x file logging
$endgroup$
add a comment |
$begingroup$
I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).
I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def rotate(self):
files = ["{}{}.{}".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]
[self.__touch(f) for f in files if not exists(f)]
current_log = "{}.{}".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return
files.insert(0, current_log)
for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])
self.__touch(files[0])
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()
python beginner python-3.x file logging
$endgroup$
$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with{self.prefix}5.{suffix}.
$endgroup$
– Peilonrayz
9 hours ago
1
$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
9 hours ago
add a comment |
$begingroup$
I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).
I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def rotate(self):
files = ["{}{}.{}".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]
[self.__touch(f) for f in files if not exists(f)]
current_log = "{}.{}".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return
files.insert(0, current_log)
for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])
self.__touch(files[0])
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()
python beginner python-3.x file logging
$endgroup$
I created a simple script that implements log rotation. It checks the log file size (say, out.log) if it exceeds 5MB limit. If it does then the script copies it to out1.log and clears out.log. But it also copies out1.log to out2.log, etc. So log files are kind of 'shifted' and contents of the last one is discarded. In this implementation I keep only 5 files (initial out.log and out[1-4].log).
I am not a Python programmer, although I find this language extremely useful for such kinds of tasks. I would like my code to be as much idiomatic as possible, so what can I change/improve in my script for this?
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def rotate(self):
files = ["{}{}.{}".format(self.prefix, x + 1, self.suffix) for x in range(MAX_LOG_FILES_COUNT)]
[self.__touch(f) for f in files if not exists(f)]
current_log = "{}.{}".format(self.prefix, self.suffix)
if (getsize(current_log) < SIZE_5MB):
return
files.insert(0, current_log)
for i in range(len(files) - 1, 0, -1):
copyfile(files[i-1], files[i])
self.__touch(files[0])
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
logRotator = LogRotator(args.prefix, args.suffix)
logRotator.rotate()
python beginner python-3.x file logging
python beginner python-3.x file logging
edited 9 hours ago
dfhwze
2,258323
2,258323
asked 10 hours ago
xueshengxuesheng
2005
2005
$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with{self.prefix}5.{suffix}.
$endgroup$
– Peilonrayz
9 hours ago
1
$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
9 hours ago
add a comment |
$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with{self.prefix}5.{suffix}.
$endgroup$
– Peilonrayz
9 hours ago
1
$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
9 hours ago
$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with
{self.prefix}5.{suffix}.$endgroup$
– Peilonrayz
9 hours ago
$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with
{self.prefix}5.{suffix}.$endgroup$
– Peilonrayz
9 hours ago
1
1
$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
9 hours ago
$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
9 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
- Return early, there's no need to touch files if the current file is <5mb.
- DRY your code, move the
"{}{}.{}".formatinto it's own function. - Don't use comprehensions for mutations. Use an explicit
forloop for that.
As you want idiomatic code, you should be aware that some people discourage the use of
__as it performs name mangling.
If the function is intended to be private, not protected, then it should be ok.
- You can use the
pairwiserecipe to make the reversed pairwise loop. I think it's more readable, however you may not. - It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.
- It's idiomatic to put two newlines around top level classes and functions.
- I'm a bit confused why you want to touch everything.
- You may want to split all the different aspects of
rotateinto smaller functions to follow SRP.
Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def _gen_file_name(self, name):
return "{}{}.{}".format(self.prefix, name, self.suffix)
def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return
files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]
for file in files:
if not exests(file):
self.__touch(f)
for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)
self.__touch(current_log)
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()
$endgroup$
$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would beopen(file_name, 'a').close()…
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed tomake_empty.
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
Also, 10. Usingos.renameinstead ofshutil.copyfilesince you don't need copies anyway.
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
8 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222089%2fsimple-log-rotation-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
- Return early, there's no need to touch files if the current file is <5mb.
- DRY your code, move the
"{}{}.{}".formatinto it's own function. - Don't use comprehensions for mutations. Use an explicit
forloop for that.
As you want idiomatic code, you should be aware that some people discourage the use of
__as it performs name mangling.
If the function is intended to be private, not protected, then it should be ok.
- You can use the
pairwiserecipe to make the reversed pairwise loop. I think it's more readable, however you may not. - It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.
- It's idiomatic to put two newlines around top level classes and functions.
- I'm a bit confused why you want to touch everything.
- You may want to split all the different aspects of
rotateinto smaller functions to follow SRP.
Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def _gen_file_name(self, name):
return "{}{}.{}".format(self.prefix, name, self.suffix)
def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return
files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]
for file in files:
if not exests(file):
self.__touch(f)
for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)
self.__touch(current_log)
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()
$endgroup$
$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would beopen(file_name, 'a').close()…
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed tomake_empty.
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
Also, 10. Usingos.renameinstead ofshutil.copyfilesince you don't need copies anyway.
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
8 hours ago
add a comment |
$begingroup$
- Return early, there's no need to touch files if the current file is <5mb.
- DRY your code, move the
"{}{}.{}".formatinto it's own function. - Don't use comprehensions for mutations. Use an explicit
forloop for that.
As you want idiomatic code, you should be aware that some people discourage the use of
__as it performs name mangling.
If the function is intended to be private, not protected, then it should be ok.
- You can use the
pairwiserecipe to make the reversed pairwise loop. I think it's more readable, however you may not. - It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.
- It's idiomatic to put two newlines around top level classes and functions.
- I'm a bit confused why you want to touch everything.
- You may want to split all the different aspects of
rotateinto smaller functions to follow SRP.
Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def _gen_file_name(self, name):
return "{}{}.{}".format(self.prefix, name, self.suffix)
def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return
files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]
for file in files:
if not exests(file):
self.__touch(f)
for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)
self.__touch(current_log)
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()
$endgroup$
$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would beopen(file_name, 'a').close()…
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed tomake_empty.
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
Also, 10. Usingos.renameinstead ofshutil.copyfilesince you don't need copies anyway.
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
8 hours ago
add a comment |
$begingroup$
- Return early, there's no need to touch files if the current file is <5mb.
- DRY your code, move the
"{}{}.{}".formatinto it's own function. - Don't use comprehensions for mutations. Use an explicit
forloop for that.
As you want idiomatic code, you should be aware that some people discourage the use of
__as it performs name mangling.
If the function is intended to be private, not protected, then it should be ok.
- You can use the
pairwiserecipe to make the reversed pairwise loop. I think it's more readable, however you may not. - It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.
- It's idiomatic to put two newlines around top level classes and functions.
- I'm a bit confused why you want to touch everything.
- You may want to split all the different aspects of
rotateinto smaller functions to follow SRP.
Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def _gen_file_name(self, name):
return "{}{}.{}".format(self.prefix, name, self.suffix)
def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return
files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]
for file in files:
if not exests(file):
self.__touch(f)
for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)
self.__touch(current_log)
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()
$endgroup$
- Return early, there's no need to touch files if the current file is <5mb.
- DRY your code, move the
"{}{}.{}".formatinto it's own function. - Don't use comprehensions for mutations. Use an explicit
forloop for that.
As you want idiomatic code, you should be aware that some people discourage the use of
__as it performs name mangling.
If the function is intended to be private, not protected, then it should be ok.
- You can use the
pairwiserecipe to make the reversed pairwise loop. I think it's more readable, however you may not. - It's unidiomatic to put brackets on if statements. Unless the statement spans multiple lines.
- It's idiomatic to put two newlines around top level classes and functions.
- I'm a bit confused why you want to touch everything.
- You may want to split all the different aspects of
rotateinto smaller functions to follow SRP.
Your code's pretty idiomatic otherwise, your naming conventions are good, you've used a main guard and you've got white space where it should be.
from os import listdir
from os.path import getsize, exists
from shutil import copyfile
from argparse import ArgumentParser
import itertools
def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = itertools.tee(iterable)
next(b, None)
return zip(a, b)
SIZE_5MB = 5e6
MAX_LOG_FILES_COUNT = 5
class LogRotator(object):
def __init__(self, prefix, suffix):
self.prefix = prefix
self.suffix = suffix
def __str__(self):
return "{}[x].{}".format(self.suffix, self.prefix)
def __touch(self, file_name):
open(file_name, 'w').close()
def _gen_file_name(self, name):
return "{}{}.{}".format(self.prefix, name, self.suffix)
def rotate(self):
current_log = self._gen_file_name('')
if getsize(current_log) < SIZE_5MB:
return
files = [
self._gen_file_name(i)
for i in range(1, MAX_LOG_FILES_COUNT + 1)
]
for file in files:
if not exests(file):
self.__touch(f)
for older, newer in pairwise(reversed([current_log] + files)):
copyfile(newer, older)
self.__touch(current_log)
if __name__ == '__main__':
parser = ArgumentParser(description="Rotate log files")
parser.add_argument("-prefix", help="log file prefix")
parser.add_argument("-suffix", help="log file suffix")
args = parser.parse_args()
log_rotator = LogRotator(args.prefix, args.suffix)
log_rotator.rotate()
answered 9 hours ago
PeilonrayzPeilonrayz
28.5k344118
28.5k344118
$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would beopen(file_name, 'a').close()…
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed tomake_empty.
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
Also, 10. Usingos.renameinstead ofshutil.copyfilesince you don't need copies anyway.
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
8 hours ago
add a comment |
$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would beopen(file_name, 'a').close()…
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed tomake_empty.
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
Also, 10. Usingos.renameinstead ofshutil.copyfilesince you don't need copies anyway.
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be
open(file_name, 'a').close()…$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
"I'm a bit confused why you want to touch everything." => I’m even more confused as it not only touch, but also clears the files. A simple touch would be
open(file_name, 'a').close()…$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed to
make_empty.$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
@MathiasEttinger That's a good point. It makes sense if it's renamed to
make_empty.$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
Also, 10. Using
os.rename instead of shutil.copyfile since you don't need copies anyway.$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
Also, 10. Using
os.rename instead of shutil.copyfile since you don't need copies anyway.$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
@MathiasEttinger Do you want to post an answer? Even if you copied your existing comments I'd upvote it :)
$endgroup$
– Peilonrayz
8 hours ago
$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
8 hours ago
$begingroup$
No, I shouldn't even be here now, I’m a bit in a hurry now ;)
$endgroup$
– Mathias Ettinger
8 hours ago
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f222089%2fsimple-log-rotation-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Could you add a short description on what this does. From my understanding of your code it finds a log that is <5mb and overwrites all files with
{self.prefix}5.{suffix}.$endgroup$
– Peilonrayz
9 hours ago
1
$begingroup$
@Peilonrayz yes, sure
$endgroup$
– xuesheng
9 hours ago