Posts By vishavjeet (66)

Install Basic software/tool/app in ubuntu/linux

1: Install Google Chrome on Ubuntu

Add Key:

> wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add -

Set repository:

> echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' | sudo tee /etc/apt/sources.list.d/google-chrome.list

Install package:

>sudo apt-get update

>sudo apt-get install google-chrome-stable

2: Install Slack from the command line with snap

> sudo snap install slack

> sudo apt-get update

> sudo apt-get upgrade slack-desktop

3: Install lampp from the command line

https://vitux.com/ubuntu-xampp/

sudo /opt/lampp/lampp start

4: Install vscode

https://code.visualstudio.com/docs/setup/linux

5: Install pycharm

sudo snap install pycharm-community --classic

6: install git

sudo apt update

sudo apt install git

git --version

How can enabled Hyper-V in window?

Step1: Open up a PowerShell or CMD session as Administrator.

Step2: Type the following command

>DISM /Online /Enable-Feature /All /FeatureName:Microsoft-Hyper-V

How can setup ssh key in window?

Open GIT Bash and type below comand

$ ssh-keygen

Copy id_rsa.pub value and add in github(ssh-key)

$ code id_rsa.pub

Add the key to the ssh-agent

eval $(ssh-agent)
ssh-add id_rsa

Vagrantfile for Docker

# -*- mode: ruby -*-
# vi: set ft=ruby :

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  config.vm.box = "geerlingguy/ubuntu2004"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine. In the example below,
  # accessing "localhost:8080" will access port 80 on the guest machine.
  # NOTE: This will enable public access to the opened port
  # config.vm.network "forwarded_port", guest: 80, host: 8080

  # Create a forwarded port mapping which allows access to a specific port
  # within the machine from a port on the host machine and only allow access
  # via 127.0.0.1 to disable public access
  # config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"

  # Create a private network, which allows host-only access to the machine
  # using a specific IP.
  # config.vm.network "private_network", ip: "192.168.33.10"

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Display the VirtualBox GUI when booting the machine
  #   vb.gui = true
  #
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Enable provisioning with a shell script. Additional provisioners such as
  # Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision "shell", inline: <<-SHELL
    sudo apt-get update
    sudo apt-get install \
    ca-certificates \
    curl \
    gnupg \
    lsb-release -y
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-achive-keyring.gpg
    echo \
    "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-achive-keyring.gpg] https://download.docker.com/linux/ubuntu \
    $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io -y
  SHELL
end

Get Logs error message, linenumber and path files using flask

  • Create Simple Log file
import logging
logging.basicConfig(filename="logs.txt",
                    filemode='a',
                    format='%(asctime)s\t%(lineno)d\t%(levelname)s\t%(pathname)s\t%(name)s\t%(message)s',
                    datefmt='%H:%M:%S',
                    level=logging.DEBUG)
msg = "NameError: name filename' is not defined "
logging.info(msg)
logging.warn(msg)
logging.error(msg)
logging.critical(msg)
  • Logs file created in logs.txt like as: 20:05:16 10 INFO C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined 20:05:16 11 WARNING C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined 20:05:16 12 ERROR C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined 20:05:16 13 CRITICAL C:\Users\vishavjeet\Documents\mywork_space\flask_demo\python_log.py root NameError: name filename' is not defined

write the code for post api using flask

from flask import Flask, request, jsonify
import os
# Create a Flask app for post api
app = Flask(__name__)
@app.route('/logs', methods= ['POST', 'GET'])
def logs_func():

    response = {
        "status": True,
        "message": "Logs data found!",
        "data":[]
    }

    if request.method == 'POST':
        file_name = request.json['filename']
        i = 1
        result = {}
        if os.path.exists(file_name):
            with open('logs.txt') as f:
                lines = f.readlines()
                for line in lines:
                    r = line.split('\t')

                    if r[2]== 'ERROR':

                        result[i] = {
                            'timestamp': r[0], 
                            'linenumber' : r[1],
                            'name': r[4],
                            'pathname': r[3],

                            'message': r[5]
                            }
                        i += 1
            response['data']= result
            return jsonify(response)
        else:
            response['status']= False
            response['message']= "File not found!"
            return jsonify(response)
    else:
        response['status']= False
        response['message']= "Only Post method is not allow"
        return jsonify(response)


if __name__ == '__main__':
    app.run(debug = True)

  • API Response:
{
    "data": {
        "1": {
            "linenumber": "12",
            "message": "NameError: name filename' is not defined \n",
            "name": "root",
            "pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
            "timestamp": "20:05:16"
        },
        "2": {
            "linenumber": "12",
            "message": "NameError: name filename' is not defined \n",
            "name": "root",
            "pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
            "timestamp": "20:05:18"
        },
        "3": {
            "linenumber": "12",
            "message": "NameError: name filename' is not defined \n",
            "name": "root",
            "pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
            "timestamp": "20:05:20"
        },
        "4": {
            "linenumber": "13",
            "message": "NameError: name filename' is not defined \n",
            "name": "root",
            "pathname": "C:\\Users\\vishavjeet\\Documents\\mywork_space\\flask_demo\\python_log.py",
            "timestamp": "20:08:12"
        }
    },
    "message": "Logs data found!",
    "status": true
}

1 2 3 Next Last