SoFunction
Updated on 2025-04-06

Summary of how to view log log commands in Linux

Dynamic real-time log viewing

Enterprise projects are all running in Linux environments. Being familiar with log viewing in Linux environments combined with remote debugging ports to promptly troubleshoot program bugs is a necessary skill that backend programmers need to master.

Tail the log fileIt is the catchphrase of the operation and maintenance classmate. The tail command is the most commonly used log viewing statement, using log filesAs an example

-f filename

tail -f displays the content at the end of the log in real time, defaults to the last 10 lines, which is equivalent to adding parameters -n 10

tail -f 

Exit tail command

Ctrl+c

tail command extension

View the last 20 lines of the log and update the log in real time

tail -f -n 20 

View the positive number of 20 lines of the log

tail -n +20 

2. Track specific content logs

The tail command can view changes in file content in real time. However, when the project log updates a lot in real time, the columns that need to be viewed often flash by. It is very inconvenient to query the key columns in a large number of logs that are quickly updated. This requires the tail command and the grep command to be used together.

Track the specific information that needs to be displayed, taking the example of viewing the fixed thread http-nio-8091-exec-7:

tail -f log_file | grep ‘http-nio-8091-exec-7'

Extended content

-C option to display the first and last lines of the search results, -A option to display the last lines of the search results, -B option to display the first lines of the search results

tail -f log_file | grep -C 5 ‘http-nio-8091-exec-7'

cat keyword search

cat is different from tail, which is a full-text search on the log, where -n is the function of displaying the line number

cat -n filename |grep “http-nio-8091-exec-7”

Extended content

-C option to display the first and last lines of the search results, -A option to display the last lines of the search results, -B option to display the first lines of the search results

cat -n log_file | grep -C 5 ‘http-nio-8091-exec-7'

View overall log operations

Order

The less command is mostly used to read text files, and can also be used to read files that have been changed in real time. ctrl +c interrupts real-time reading and display, pressing q will exit the less view

Option +F can track file changes in real time:

less +F log_file
  • Step 1: Open the log file
less 
  • Step 2: Locate the last line of the log file:
shift+g Move to the last line
  • Step 3:
ctrl+b Turn the page one by one to view

refer to:

1. Full screen navigation

  • ctrl + F - Move forward one screen
  • ctrl + B - Move one screen backward
  • ctrl + D - Move half screen forward
  • ctrl + U - Move half the screen backwards

2. Single-line navigation

  • j - Move forward one line
  • k - Move one line backward

3. Other navigation

  • G - Move to the last line
  • g - Move to first row
  • q / ZZ - Exit less command

Edit mode to view logs

For editing mode operation log files, it is also a good way to be familiar with the process.

  • /Keyword Note: Forward search, press n key to move the cursor to the next place that meets the conditions.
  • ?Keyword Note: Reverse search, press shift+n key to move the cursor to the next one that meets the criteria

Download log files

1. Get the container number in docker

Viewing logs through docker requires you to know the container number CONTAINER ID in docker first.

Pass the commandsudo docker psorsudo docker container ls -aAny one is OK

2. Entering our project, there will be a log file that records the name path of the log file we need.

sudo docker exec -it CONTAINER ID sh

3. Exit the docker container

exit

4. Copy the file path found in step 2 in the container to any directory in the server

sudo docker container cp CONTAINER ID:/opt/logs/2020-02/ /home/brain/

This is a bit troublesome, I still hope to mount the server directory file from the beginning

Or use the remote connection tool to drag and drop download

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.