Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3942527292 | ||
|
|
46687f004e | ||
|
|
ad2edf098d | ||
|
|
b7f06adce4 | ||
|
|
de6becc208 | ||
|
|
917624ad52 | ||
|
|
e9c060cd0d |
2
.travis.yml
Normal file
2
.travis.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
language: csharp
|
||||||
|
solution: CloudWatchLogDownloader.sln
|
||||||
@@ -56,6 +56,7 @@
|
|||||||
<Reference Include="System.Xml" />
|
<Reference Include="System.Xml" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="DebugLogger.cs" />
|
||||||
<Compile Include="Models\CommandOptions.cs" />
|
<Compile Include="Models\CommandOptions.cs" />
|
||||||
<Compile Include="Program.cs" />
|
<Compile Include="Program.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
|
|||||||
87
CloudWatchLogDownloader/DebugLogger.cs
Normal file
87
CloudWatchLogDownloader/DebugLogger.cs
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
namespace CloudWatchLogDownloader
|
||||||
|
{
|
||||||
|
internal static class DebugLogger
|
||||||
|
{
|
||||||
|
private static bool _debug;
|
||||||
|
public static bool Debug
|
||||||
|
{
|
||||||
|
get { return _debug; }
|
||||||
|
set
|
||||||
|
{
|
||||||
|
if (value && !_debug)
|
||||||
|
{
|
||||||
|
WriteLine("debug mode active");
|
||||||
|
}
|
||||||
|
_debug = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Write(string str)
|
||||||
|
{
|
||||||
|
if (_debug)
|
||||||
|
{
|
||||||
|
var oldcolor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.Write(str);
|
||||||
|
Console.ForegroundColor = oldcolor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Write(object obj)
|
||||||
|
{
|
||||||
|
if (_debug)
|
||||||
|
{
|
||||||
|
var oldcolor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.Write(obj);
|
||||||
|
Console.ForegroundColor = oldcolor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Write(string format, params object[] args)
|
||||||
|
{
|
||||||
|
if (_debug)
|
||||||
|
{
|
||||||
|
var oldcolor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.Write(format, args);
|
||||||
|
Console.ForegroundColor = oldcolor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteLine(string str)
|
||||||
|
{
|
||||||
|
if (_debug)
|
||||||
|
{
|
||||||
|
var oldcolor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.WriteLine(str);
|
||||||
|
Console.ForegroundColor = oldcolor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteLine(object obj)
|
||||||
|
{
|
||||||
|
if (_debug)
|
||||||
|
{
|
||||||
|
var oldcolor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.WriteLine(obj);
|
||||||
|
Console.ForegroundColor = oldcolor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void WriteLine(string format, params object[] args)
|
||||||
|
{
|
||||||
|
if (_debug)
|
||||||
|
{
|
||||||
|
var oldcolor = Console.ForegroundColor;
|
||||||
|
Console.ForegroundColor = ConsoleColor.Cyan;
|
||||||
|
Console.WriteLine(format, args);
|
||||||
|
Console.ForegroundColor = oldcolor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,9 @@ namespace CloudWatchLogDownloader.Models
|
|||||||
[Option('l', "liveStream", HelpText="Keep pulling logs until Ctrl+C is applied")]
|
[Option('l', "liveStream", HelpText="Keep pulling logs until Ctrl+C is applied")]
|
||||||
public bool LiveStream { get; set; }
|
public bool LiveStream { get; set; }
|
||||||
|
|
||||||
|
[Option('d',"debug", HelpText = "print additional logs to console")]
|
||||||
|
public bool Debug { get; set; }
|
||||||
|
|
||||||
[ParserState]
|
[ParserState]
|
||||||
public IParserState ParserState { get; set; }
|
public IParserState ParserState { get; set; }
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using Amazon;
|
|
||||||
|
|
||||||
namespace CloudWatchLogDownloader
|
namespace CloudWatchLogDownloader
|
||||||
{
|
{
|
||||||
@@ -17,6 +16,8 @@ namespace CloudWatchLogDownloader
|
|||||||
var opt = new CommandOptions();
|
var opt = new CommandOptions();
|
||||||
if (CommandLine.Parser.Default.ParseArguments(args, opt))
|
if (CommandLine.Parser.Default.ParseArguments(args, opt))
|
||||||
{
|
{
|
||||||
|
DebugLogger.Debug = opt.Debug;
|
||||||
|
|
||||||
client = new AmazonCloudWatchLogsClient();
|
client = new AmazonCloudWatchLogsClient();
|
||||||
var logGroup = GetLogGroup(opt.LogGroup);
|
var logGroup = GetLogGroup(opt.LogGroup);
|
||||||
var logStream = GetLogStream(logGroup, opt.LogStream);
|
var logStream = GetLogStream(logGroup, opt.LogStream);
|
||||||
@@ -30,8 +31,10 @@ namespace CloudWatchLogDownloader
|
|||||||
DescribeLogGroupsResponse lgResponse = null;
|
DescribeLogGroupsResponse lgResponse = null;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
DebugLogger.WriteLine("Getting logGroups...");
|
||||||
lgResponse = client.DescribeLogGroups(new DescribeLogGroupsRequest { NextToken = (lgResponse != null ? lgResponse.NextToken : null) });
|
lgResponse = client.DescribeLogGroups(new DescribeLogGroupsRequest { NextToken = (lgResponse != null ? lgResponse.NextToken : null) });
|
||||||
allGroups.AddRange(lgResponse.LogGroups);
|
allGroups.AddRange(lgResponse.LogGroups);
|
||||||
|
DebugLogger.WriteLine("Got logGroups, have {0}, {1} more pages", allGroups.Count, (!string.IsNullOrWhiteSpace(lgResponse.NextToken) ? "still" : "no"));
|
||||||
} while (!string.IsNullOrWhiteSpace(lgResponse.NextToken));
|
} while (!string.IsNullOrWhiteSpace(lgResponse.NextToken));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(logGroup) || logGroup[logGroup.Length - 1] == '*')
|
if (string.IsNullOrWhiteSpace(logGroup) || logGroup[logGroup.Length - 1] == '*')
|
||||||
@@ -65,12 +68,14 @@ namespace CloudWatchLogDownloader
|
|||||||
DescribeLogStreamsResponse lsResponse = null;
|
DescribeLogStreamsResponse lsResponse = null;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
DebugLogger.WriteLine("Getting logStreams...");
|
||||||
lsResponse = client.DescribeLogStreams(new DescribeLogStreamsRequest
|
lsResponse = client.DescribeLogStreams(new DescribeLogStreamsRequest
|
||||||
{
|
{
|
||||||
NextToken = (lsResponse != null ? lsResponse.NextToken : null),
|
NextToken = (lsResponse != null ? lsResponse.NextToken : null),
|
||||||
LogGroupName = logGroup.LogGroupName
|
LogGroupName = logGroup.LogGroupName
|
||||||
});
|
});
|
||||||
allStreams.AddRange(lsResponse.LogStreams);
|
allStreams.AddRange(lsResponse.LogStreams);
|
||||||
|
DebugLogger.WriteLine("Got logStreams, have {0}, {1} more pages", allStreams.Count, (!string.IsNullOrWhiteSpace(lsResponse.NextToken) ? "still" : "no"));
|
||||||
} while (!string.IsNullOrWhiteSpace(lsResponse.NextToken));
|
} while (!string.IsNullOrWhiteSpace(lsResponse.NextToken));
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(logStream) || logStream[logStream.Length - 1] == '*')
|
if (string.IsNullOrWhiteSpace(logStream) || logStream[logStream.Length - 1] == '*')
|
||||||
@@ -101,11 +106,20 @@ namespace CloudWatchLogDownloader
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void WriteLogToFile(LogGroup logGroup, LogStream logStream, bool liveStream, string outputFilePath = null)
|
private static void WriteLogToFile(LogGroup logGroup, LogStream logStream, bool liveStream, string outputFilePath = null)
|
||||||
|
{
|
||||||
|
string output = null;
|
||||||
|
if (string.IsNullOrWhiteSpace(outputFilePath))
|
||||||
{
|
{
|
||||||
Console.WriteLine("Choose Output file [logs/" + logStream.LogStreamName + ".log]: ");
|
Console.WriteLine("Choose Output file [logs/" + logStream.LogStreamName + ".log]: ");
|
||||||
var output = Console.ReadLine();
|
output = Console.ReadLine();
|
||||||
if (string.IsNullOrWhiteSpace(output))
|
if (string.IsNullOrWhiteSpace(output))
|
||||||
output = "logs/" + logStream.LogStreamName + ".log";
|
output = "logs/" + logStream.LogStreamName + ".log";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
output = outputFilePath;
|
||||||
|
}
|
||||||
|
|
||||||
if (!Directory.GetParent(output).Exists)
|
if (!Directory.GetParent(output).Exists)
|
||||||
Directory.CreateDirectory(Directory.GetParent(output).FullName);
|
Directory.CreateDirectory(Directory.GetParent(output).FullName);
|
||||||
|
|
||||||
@@ -118,6 +132,7 @@ namespace CloudWatchLogDownloader
|
|||||||
GetLogEventsResponse leResponse = null;
|
GetLogEventsResponse leResponse = null;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
DebugLogger.WriteLine("Getting events...");
|
||||||
leResponse = client.GetLogEvents(new GetLogEventsRequest
|
leResponse = client.GetLogEvents(new GetLogEventsRequest
|
||||||
{
|
{
|
||||||
LogGroupName = logGroup.LogGroupName,
|
LogGroupName = logGroup.LogGroupName,
|
||||||
@@ -125,13 +140,14 @@ namespace CloudWatchLogDownloader
|
|||||||
StartFromHead = true,
|
StartFromHead = true,
|
||||||
NextToken = (leResponse != null ? leResponse.NextForwardToken : null)
|
NextToken = (leResponse != null ? leResponse.NextForwardToken : null)
|
||||||
});
|
});
|
||||||
|
DebugLogger.WriteLine("Got {0} events", leResponse.Events.Count);
|
||||||
|
|
||||||
foreach (var ev in leResponse.Events)
|
foreach (var ev in leResponse.Events)
|
||||||
sw.WriteLine(ev.Message);
|
sw.WriteLine(ev.Message);
|
||||||
|
|
||||||
sw.Flush();
|
sw.Flush();
|
||||||
|
|
||||||
if (!leResponse.Events.Any() && !lsMessage)
|
if (!leResponse.Events.Any() && !lsMessage && liveStream)
|
||||||
{
|
{
|
||||||
lsMessage = true;
|
lsMessage = true;
|
||||||
ConsoleColor oldcolor = Console.ForegroundColor;
|
ConsoleColor oldcolor = Console.ForegroundColor;
|
||||||
|
|||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2016 gardient
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
# CloudWatchLogDownloader
|
# CloudWatchLogDownloader
|
||||||
Download full logs from AWS CloudWatch
|
Download full logs from AWS CloudWatch
|
||||||
|
|
||||||
|
[](https://travis-ci.org/gardient/CloudWatchLogDownloader)
|
||||||
|
|
||||||
## Who is it for?
|
## Who is it for?
|
||||||
For idiots like me who just want a text dump of the cloud watch log without going through S3
|
For idiots like me who just want a text dump of the cloud watch log without going through S3
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user