jeffkpayne commented on a change in pull request #3992: [AIRFLOW-620] Feature 
to tail custom number of logs instead of rendering whole log
URL: https://github.com/apache/incubator-airflow/pull/3992#discussion_r223222849
 
 

 ##########
 File path: airflow/bin/cli.py
 ##########
 @@ -1023,17 +1023,41 @@ def scheduler(args):
 @cli_utils.action_logging
 def serve_logs(args):
     print("Starting flask")
-    import flask
-    flask_app = flask.Flask(__name__)
+    from flask import Flask, request, Response, stream_with_context, 
send_from_directory
+    flask_app = Flask(__name__)
 
     @flask_app.route('/log/<path:filename>')
     def serve_logs(filename):  # noqa
+        def tail_logs(logdir, filename, num_lines):
+            logpath = "{logdir}/{filename}".format(logdir=logdir, 
filename=filename)
+            logsize = os.path.getsize(logpath)
+            if logsize >= 100 * 1024 * 1024:
+                p1 = subprocess.Popen(["tail", "-n " + str(num_lines), 
filename],
+                                      stdout=subprocess.PIPE, cwd=log)
+                out, err = p1.communicate()
+                out = "Tailing file\n\n" + out.decode("utf-8")
+            else:
+                fl = open("{log}//{filename}".format(log=log, 
filename=filename), "r")
+                lines = fl.readlines()
+                fl.close()
+                out = "".join(l for l in lines[-num_lines:])
+            line = "***** Showing only last {num_lines} lines from {filename} 
*****" \
+                   "\n\n\n{out}".format(num_lines=num_lines, 
filename=filename, out=out)
+            yield line
+        num_lines = request.args.get("num_lines")
+        try:
+            num_lines = int(num_lines)
+        except ValueError or TypeError:
+            num_lines = None
 
 Review comment:
   If `num_lines` is not a valid `int`, should something be logged so the 
user/admin knows that something it setup incorrectly?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to