5 Commits

Author SHA1 Message Date
Gardient
e9c060cd0d #1 - fixes ignored command line argument 2016-11-10 13:47:37 +02:00
Gardient
acf3f8c0fe Updates README.md 2016-10-27 15:59:05 +03:00
Gardient
ea09bd6b19 Merge branch 'master' of github.com:gardient/CloudWatchLogDownloader 2016-10-27 15:53:58 +03:00
Gardient
2bfa977d99 Adds support for livestreaming logstreams 2016-10-27 15:51:04 +03:00
gardient
ddbfa25f8e Create README.md 2016-10-21 14:58:25 +03:00
3 changed files with 58 additions and 8 deletions

View File

@@ -17,6 +17,9 @@ namespace CloudWatchLogDownloader.Models
[ValueOption(2)]
public string OutputFilePath { get; set; }
[Option('l', "liveStream", HelpText="Keep pulling logs until Ctrl+C is applied")]
public bool LiveStream { get; set; }
[ParserState]
public IParserState ParserState { get; set; }

View File

@@ -19,7 +19,7 @@ namespace CloudWatchLogDownloader
client = new AmazonCloudWatchLogsClient();
var logGroup = GetLogGroup(opt.LogGroup);
var logStream = GetLogStream(logGroup, opt.LogStream);
WriteLogToFile(logGroup, logStream, opt.OutputFilePath);
WriteLogToFile(logGroup, logStream, opt.LiveStream, opt.OutputFilePath);
}
}
@@ -80,7 +80,7 @@ namespace CloudWatchLogDownloader
allStreams = allStreams.Where(x => x.LogStreamName.StartsWith(logStream)).ToList();
}
allStreams = allStreams.OrderBy(x => x.CreationTime).ToList();
allStreams = allStreams.OrderByDescending(x => x.CreationTime).ToList();
for (int i = 0, len = allStreams.Count; i < len; ++i)
Console.WriteLine(i + ") " + allStreams[i].LogStreamName);
@@ -99,15 +99,28 @@ namespace CloudWatchLogDownloader
return ls;
}
private static void WriteLogToFile(LogGroup logGroup, LogStream logStream, string outputFilePath = null)
private static void WriteLogToFile(LogGroup logGroup, LogStream logStream, bool liveStream, string outputFilePath = null)
{
Console.WriteLine("Choose Output file [logs/" + logStream.LogStreamName + ".log]: ");
var output = Console.ReadLine();
if (string.IsNullOrWhiteSpace(output))
output = "logs/" + logStream.LogStreamName + ".log";
string output = null;
if (string.IsNullOrWhiteSpace(outputFilePath))
{
Console.WriteLine("Choose Output file [logs/" + logStream.LogStreamName + ".log]: ");
output = Console.ReadLine();
if (string.IsNullOrWhiteSpace(output))
output = "logs/" + logStream.LogStreamName + ".log";
}
else
{
output = outputFilePath;
}
if (!Directory.GetParent(output).Exists)
Directory.CreateDirectory(Directory.GetParent(output).FullName);
Console.WriteLine("Downloading log into " + output);
bool lsMessage = !liveStream;
using (StreamWriter sw = new StreamWriter(output))
{
GetLogEventsResponse leResponse = null;
@@ -125,7 +138,17 @@ namespace CloudWatchLogDownloader
sw.WriteLine(ev.Message);
sw.Flush();
} while (leResponse.NextForwardToken != null && leResponse.Events.Any());
if (!leResponse.Events.Any() && !lsMessage)
{
lsMessage = true;
ConsoleColor oldcolor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Now streaming live from lg: " + logGroup.LogGroupName + " ls: " + logStream.LogStreamName + " into " + output);
Console.ForegroundColor = oldcolor;
Console.WriteLine("{0}{0}Press CTRL+C to stop...", Environment.NewLine);
}
} while ((leResponse.NextForwardToken != null && leResponse.Events.Any()) || liveStream);
}
}

24
README.md Normal file
View File

@@ -0,0 +1,24 @@
# CloudWatchLogDownloader
Download full logs from AWS CloudWatch
## Who is it for?
For idiots like me who just want a text dump of the cloud watch log without going through S3
## Why .NET?
Because I was working with the .NET AWSSDK when writing this.
## Why not .NET CORE?
Because the AWSSDK.CloudWatchLogs nuget package doesn't support it. (As of writing this README)
## Usage
If you're still reading this means you want to use this abomination... Good.
### Command line params
|short|long|Description|
|-----|----|-----------|
|-g|--logGroup|The log group you want to select the stream from, can end with '*' in which case the option to choose from all groups starting with this will be given|
|-s|--logStream|The log stream you want to save, can end with '*' in which case the option to choose from all groups starting with this will be given|
|-o|--outputFile|The file to output the logs to.|
|-l|--liveStream|After reaching the newest log keep polling until ctrl+c is pressed|
If any of the above are not specified the program will prompt for them