I have had gnat issues in my brewery before and they are a serious pain. Apparently, they are attracted to the aroma of fermentation. (I suppose you can’t blame them for that). I discovered them in my airlock and fortunately, they were not able to pass into my brew.
To get rid of gnats, you need to cut very small pieces of apple (1/2” X 1/2”) and place a piece into a jar or cup. Then add some cider vinegar until it’s half way up the piece of apple. Then cover the jar with foil and a tight rubber band. Then poke some very small holes in the aluminum foil. This is a trap. They are attracted to the sent, but can’t get out. Once they hit the vinegar, it’s over.
Now I ferment in a temperature controlled fermentation chamber and my problem is solved.